我有这个带有键值对的json,我想知道在那个json文件中出现了多少次键值对完全匹配
这是jquery代码
$(document).ready(function(){
var data = [{"programmer":"yes","married": "no", "does_java": "no"},{"programmer":"yes","married": "no", "does_java": "no"},{"programmer":"yes","married": "no", "does_java": "yes"},{"programmer":"yes","married": "no", "does_java": "no"},{"programmer":"yes","married": "no", "does_java": "yes"},{"programmer":"yes","married": "no", "does_java": "no"},{"programmer":"yes","married": "no", "does_java": "yes"},{"programmer":"yes","married": "no", "does_java": "no"},{"programmer":"yes","married": "no", "does_java": "no"},{"programmer":"yes","married": "no", "does_java": "no"},{"programmer":"yes","married": "no", "does_java": "no"}];
//get the number of times a key:value pair occurs in json
/**
I am trying to know the number of times a specific key:value pair occurs in this json.
*/
$.each(data,function(k,v){
if(v['does_java'] === 'yes'){
console.log($(data).find(v['does_java'] === 'yes').length);
//expecting 3,but displays 0
}
});
});
键值does_java:yes
出现3次,但在我的情况下,它显示为零。我该如何解决此问题?。
答案 0 :(得分:1)
试试这个
...
var javaDo = 0;
$.each(data,function(k,v){
if(v['does_java'] === 'yes'){
javaDo++;
}
});
console.log(javaDo);
答案 1 :(得分:0)
试试这个,
keyarr = []
$.each(data,function(k,v){
if(v['does_java'] === 'yes'){
keyarr.push(v['does_java']);
}
});
alert(keyarr.length)