2

I have a json file with players names on it as properties and the values are yes and no. I'm trying to add one to count every time the (Property = 'yes), player plays.

Here an example of the json file

{"Wojciech Szczesny":"yes","Lukasz Fabianski":"no","Emiliano Viviano":"no","Olivier Giroud":"yes","Per Mertesacker":"yes","Bacary Sagna":"yes","Laurent Koscielny":"no","Santi Cazorla":"yes","Mikel Arteta":"yes","Mesut \u00d6zil":"no","Kieran Gibbs":"yes","Aaron Ramsey":"no","Jack Wilshere":"no","Mathieu Flamini":"yes","Tomas Rosicky":"yes","Lukas Podolski":"yes","Nacho Monreal":"no","Theo Walcott":"no","Thomas Vermaelen":"yes","Carl Jenkinson":"no","Alex Oxlade-Chamberlain":"no","Serge Gnabry":"no","Kim Kallstrom":"no","Nicklas Bendtner":"no","Abou Diaby":"no","Park Chu-Young":"no","Emmanuel Frimpong":"no","Yaya Sanogo":"no","Ryo Miyaichi":"no","Hector Bellerin":"no","Chuba Akpom":"no","Isaac Hayden":"no","Gideon Zelalem":"no","Home":"home","Results":"draw"}

And here my Jquery code

var count = 0;
    $.ajax({
        url:'ars.json',
        dataType:'json',
        cache: true,
  success: function(data) {
    $(data).each(function(index, value){
        if(Object.getOwnPropertyNames (value) == 'yes'){
            count++;
        }else{

        }
    });
    console.log(count);
}
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
user3210416
  • 794
  • 1
  • 10
  • 20
  • Just `value == 'yes'`??? – Bergi May 12 '14 at 16:06
  • 2
    See also [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call): You're logging `count` before it will get incremented. – Bergi May 12 '14 at 16:08

3 Answers3

1

http://jsbin.com/CountSamePropertyValues/1/edit?js,console,output

var count = 0;
$.ajax({
  url:'ars.json',
  dataType:'json',
  cache: true,
  success: function(data) {
    for(var k in data){
      if(data.hasOwnProperty(k) && data[k]=="yes"){
         count++;
      }
    }
    console.log(count); // 11
  }
});
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
1

As I got it you want to count the properties with value 'yes'... You did two mistakes - which made your code completely wrong: 1. You are using Object instead of this inside the loop, of course Object has no any of the properties you are looking for... 2. $(data).each runs only once as data is not an array but a single object Now for the right code:

success: function(data) {
  var count = 0;
  for(value in data) {
    if(data[value] == 'yes') {
      count++;
    } else {
      //
    }
});
0

Try:

var v = {"Wojciech Szczesny":"yes","Lukasz Fabianski":"no","Emiliano Viviano":"no","Olivier Giroud":"yes","Per Mertesacker":"yes","Bacary Sagna":"yes","Laurent Koscielny":"no","Santi Cazorla":"yes","Mikel Arteta":"yes","Mesut \u00d6zil":"no","Kieran Gibbs":"yes","Aaron Ramsey":"no","Jack Wilshere":"no","Mathieu Flamini":"yes","Tomas Rosicky":"yes","Lukas Podolski":"yes","Nacho Monreal":"no","Theo Walcott":"no","Thomas Vermaelen":"yes","Carl Jenkinson":"no","Alex Oxlade-Chamberlain":"no","Serge Gnabry":"no","Kim Kallstrom":"no","Nicklas Bendtner":"no","Abou Diaby":"no","Park Chu-Young":"no","Emmanuel Frimpong":"no","Yaya Sanogo":"no","Ryo Miyaichi":"no","Hector Bellerin":"no","Chuba Akpom":"no","Isaac Hayden":"no","Gideon Zelalem":"no","Home":"home","Results":"draw"};

var count = 0;
$.each(v, function(k,v) {
    if (v == "yes") count++;
});

console.log(count);

DEMO

In your case:

var count = 0;
$.ajax({
        url: 'ars.json',
        dataType: 'json',
        cache: true,
        success: function (data) {
            $.each(data, function (index, value) {
                if (value == "yes") count++;
            });
            console.log(count);
        }
});
martynas
  • 12,120
  • 3
  • 55
  • 60