1

I have this json with key value pairs and i am trying to know how many times that exact match of that key value pairs occurs in that json file

This is the jquery code

$(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
}
});
});

The key value does_java:yes appears 3 times but in my case it displays zero.How should i go about fixing this?.

  • 1
    Jquery `find` doesn't work the way you think it does, look [here](http://stackoverflow.com/q/4992383/1592398) – code-jaff Feb 08 '14 at 06:22

2 Answers2

1

Try this

...

var javaDo = 0;
$.each(data,function(k,v){
    if(v['does_java'] === 'yes'){
        javaDo++;
    }
});

console.log(javaDo);
Felipe Pereira
  • 11,410
  • 4
  • 41
  • 45
0

Try this,

JsFiddle

 keyarr = []
$.each(data,function(k,v){

if(v['does_java'] === 'yes'){
keyarr.push(v['does_java']);
}
});

    alert(keyarr.length)
dhana
  • 6,487
  • 4
  • 40
  • 63