Reputation:
I want to check if a value doesn't exist in the given object, by filtering an array of string.
I want to check if the values in the keys
array are contained in the JSON object I'm looping. If one of the values isn't, I have to do something else, but only if the non-existent value (in resArray
) is contained in the keys
array.
Here's what I tried:
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'});
}
}
Thanks to everyone has replied to this thread.
Upvotes: 1
Views: 2898
Reputation: 10464
Your example insinuates that you're creating a new array based off the stats and conditional presence of your provided keys. An easy way to build this array would be to use Array.prototype.map
to enumerate over your stats array. Next, in each iteration's callback you can pass the name
property as an argument to keys.indexOf
to check if that particular name
is present in your keys
array.
var statsArray = stats.map(function(stat) {
if (keys.indexOf(stat.name) > -1) {
return stat;
} else {
return stat.name + ' not found.';
}
});
This will yield a new array which will contain either the stat object or a not regarding its absence in keys. However, you can return whatever your heart desires, as long as it's a valid array item.
Here's a working example with a small chunk of your dataset (but will work with your original dataset):
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);
Upvotes: 1
Reputation: 33209
You can achieve what you want by using a combination of array functions. For example:
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);
Then you can just loop through the matches/negatives to do what you want with them.
Fiddle here.
Upvotes: 0