具有匹配值jquery js的对象属性

时间:2014-05-12 16:03:45

标签: javascript jquery json

我有一个json文件,其中包含玩家名称作为属性,值为yes和no。我每次尝试(Property =' yes)玩家时都会尝试添加一个。

这里是json文件的一个例子

  

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

这里是我的Jquery代码

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

3 个答案:

答案 0 :(得分: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
  }
});

答案 1 :(得分:1)

当我得到它时,你想要计算具有价值的属性'是' ... 你犯了两个错误 - 这使你的代码完全错误: 1.你在循环中使用Object代替这个,当然Object没有你要寻找的任何属性...... 2. $(data).each只运行一次,因为数据不是数组而是单个对象 现在为正确的代码:

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

答案 2 :(得分:0)

尝试:

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

在您的情况下:

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