Simplest way to get count of keys with a particular value

前端 未结 4 555
太阳男子
太阳男子 2021-01-25 05:34

Given a javascript object, what is the simplest way to get the number of keys with a particular value?

For example, I have the following javascript obje

4条回答
  •  轮回少年
    2021-01-25 06:19

    var result = {1: "PASS", 2: "PASS", 3: "FAIL", 4: "PASS", 5: "FAIL"};
    
    undefined
    
    function getCountByValue(obj,value){
      return Object.keys(result).filter(function(key){
      return result[key] === value
    
    }).length
    
    }
    
    //as your wish
    getCountByValue(result,'FAIL')
    getCountByValue(result,'PASS')

提交回复
热议问题