检查JSON对象中是否存在值

时间:2016-08-30 23:11:29

标签: javascript arrays json object

我想通过过滤字符串数组来检查给定对象中是否存在值。

我想检查keys数组中的值是否包含在我循环的JSON对象中。如果其中一个值不是,则必须执行其他操作,但前提是resArray数组中包含不存在的值(keys中)。

JSON here

这是我尝试的内容:

var keys = [
    "total_kills",
    "total_deaths",
    "total_planted_bombs",
    "total_defused_bombs",
    "total_kills_knife",
    "total_kills_headshot",
    "total_wins_pistolround",
    "total_wins_map_de_dust2",
    "last_match_wins",
    "total_shots_fired",
    "total_shots_hit",
    "total_rounds_played",
    "total_kills_taser",
    "last_match_kills",
    "last_match_deaths",
    "total_kills_hegrenade",
  ];

  var resArray = stats.playerstats.stats;
  var statsArray = [];

  for (var i = 0; i < keys.length; i++) {
    for(var j = 0; j < resArray.length; j++){

        //if the value in keys array exists, do something
        if(resArray[j]["name"] === keys[i]){
         //do something
       }
       if(<value doesn't exist)>)
         //do something else.
    }
  }

解决:

function contains(obj, key, value) {
        return obj.hasOwnProperty(key) && obj[key] === value;
      } 

      var resArray = stats.playerstats.stats;
      var statsArray = [];

      for (var i = 0; i < keys.length; i++) {

        resArray.some(function(found){

          if(contains(found, "name", keys[i])){

            statsArray.push(found);

          }

        });

        if(typeof statsArray[i] == 'undefined'){

          console.log("Not present in array: " + keys[i]);
          statsArray.push({"name": keys[i], "value": 'None'});

        }
      }

感谢大家回复了这个帖子。

2 个答案:

答案 0 :(得分:1)

您的示例暗示您根据所提供密钥的统计信息和条件存在创建新阵列。构建此数组的一种简单方法是使用Array.prototype.map枚举您的stats数组。接下来,在每次迭代的回调中,您可以将name属性作为参数传递给keys.indexOf,以检查name数组中是否存在特定的keys

var statsArray = stats.map(function(stat) {
  if (keys.indexOf(stat.name) > -1) {
    return stat;
  } else {
    return stat.name + ' not found.';
  }
});

这将生成一个新数组,该数组将包含stat对象或不包含其在键中的缺失。但是,只要它是一个有效的数组项,你就可以回复你想要的任何东西。

这是一个包含一小部分数据集的工作示例(但可以使用原始数据集):

&#13;
&#13;
var keys = [
  "total_kills",
  "total_deaths",
  "total_planted_bombs",
  "total_defused_bombs",
  "total_kills_knife",
  "total_kills_headshot",
  "total_wins_pistolround",
  "total_wins_map_de_dust2",
  "last_match_wins",
  "total_shots_fired",
  "total_shots_hit",
  "total_rounds_played",
  "total_kills_taser",
  "last_match_kills",
  "last_match_deaths",
  "total_kills_hegrenade",
];

var stats = [{
  "name": "total_kills",
  "value": 25305
}, {
  "name": "total_deaths",
  "value": 27474
}, {
  "name": "total_time_played",
  "value": 1822419
}, {
  "name": "total_planted_bombs",
  "value": 1397
}, {
  "name": "total_defused_bombs",
  "value": 239
}, {
  "name": "total_wins",
  "value": 11477
}, {
  "name": "total_damage_done",
  "value": 3783962
}, {
  "name": "total_money_earned",
  "value": 65159500
}, {
  "name": "total_rescued_hostages",
  "value": 1
}, {
  "name": "total_kills_knife",
  "value": 278
}, {
  "name": "total_kills_hegrenade",
  "value": 168
}, {
  "name": "total_kills_glock",
  "value": 699
}, {
  "name": "total_kills_deagle",
  "value": 1289
}, {
  "name": "total_kills_elite",
  "value": 37
}, {
  "name": "total_kills_fiveseven",
  "value": 165
}, {
  "name": "total_kills_xm1014",
  "value": 78
}, {
  "name": "total_kills_mac10",
  "value": 154
}, {
  "name": "total_kills_ump45",
  "value": 330
}, {
  "name": "total_kills_p90",
  "value": 1105
}, {
  "name": "total_kills_awp",
  "value": 6934
}, {
  "name": "total_kills_ak47",
  "value": 4528
}, {
  "name": "total_kills_aug",
  "value": 137
}, {
  "name": "total_kills_famas",
  "value": 540
}, {
  "name": "total_kills_g3sg1",
  "value": 116
}, {
  "name": "total_kills_m249",
  "value": 50
}, {
  "name": "total_kills_headshot",
  "value": 7112
}, {
  "name": "total_kills_enemy_weapon",
  "value": 2308
}, {
  "name": "total_wins_pistolround",
  "value": 843
}, {
  "name": "total_wins_map_cs_assault",
  "value": 9
}, {
  "name": "total_wins_map_cs_italy",
  "value": 15
}, {
  "name": "total_wins_map_cs_office",
  "value": 11
}, {
  "name": "total_wins_map_de_aztec",
  "value": 71
}, {
  "name": "total_wins_map_de_cbble",
  "value": 373
}, {
  "name": "total_wins_map_de_dust2",
  "value": 4857
}, {
  "name": "total_wins_map_de_dust",
  "value": 25
}, {
  "name": "total_wins_map_de_inferno",
  "value": 777
}, {
  "name": "total_wins_map_de_nuke",
  "value": 247
}, {
  "name": "total_wins_map_de_train",
  "value": 47
}, {
  "name": "total_weapons_donated",
  "value": 2466
}, {
  "name": "total_broken_windows",
  "value": 30
}, {
  "name": "total_kills_enemy_blinded",
  "value": 566
}, {
  "name": "total_kills_knife_fight",
  "value": 67
}, {
  "name": "total_kills_against_zoomed_sniper",
  "value": 2284
}, {
  "name": "total_dominations",
  "value": 270
}, {
  "name": "total_domination_overkills",
  "value": 225
}, {
  "name": "total_revenges",
  "value": 207
}, {
  "name": "total_shots_hit",
  "value": 83704
}, {
  "name": "total_shots_fired",
  "value": 399207
}, {
  "name": "total_rounds_played",
  "value": 23419
}, {
  "name": "total_shots_deagle",
  "value": 12137
}, {
  "name": "total_shots_glock",
  "value": 21299
}, {
  "name": "total_shots_elite",
  "value": 777
}, {
  "name": "total_shots_fiveseven",
  "value": 3385
}, {
  "name": "total_shots_awp",
  "value": 22667
}];

var statsArray = stats.map(function(stat) {
  if(keys.indexOf(stat.name) > -1) {
    return stat;
  } else {
    return stat.name + ' not present in keys';
  }
});

console.log(statsArray);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

使用数组函数的组合可以实现您想要的效果。例如:

let stats = data.playerstats.stats;
let matches = stats.filter(i => keys.indexOf(i.name) >= 0);
let matchKeys = matches.map(k => k.name);
let negatives = keys.filter(i => matchKeys.indexOf(i) < 0);

然后你可以循环完成匹配/否定,以便用它们做你想做的事。

小提琴here