var eachMapping = [{"fname":"John"},{"fname":"John"}];
var count = 0;


    for(var i = 0; i < eachMapping.length; i++){
        for(var prop in eachMapping[i]){
            if(eachMapping[i][prop] = "fname"){
               count+=1
            }
        }
}

console.log(count);

最佳答案

首先:使用单个“ =”更改eachMapping [i] [prop]的值,而不是检查相似性。尝试将其更改为“ ==“

第二:U不需要双循环。尝试:

for(var i = 0; i < eachMapping.length; i++){
   if(eachMapping[i]["fname"]){
       count+=1
    }


}

关于javascript - 我在Javascript中有一个对象数组。如何遍历它以查找'fname'键的出现次数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42600975/

10-12 23:36