I have JSON data that I want to filter that is an array type.
{
"1": {
"enemy_id": 1,
"ai_": 0,
"ailmentcast1": -1
},
"9": {
"enemy_id": 2,
"ai_": 5,
"ailmentcast1": 2
},
"2": {
"enemy_id": 4,
"ai_": 10,
"ailmentcast1": 7
},
"29": {
"enemy_id": 1,
"ai_": 1,
"ailmentcast1": 15
},
"17": {
"enemy_id": 3,
"ai_": 7,
"ailmentcast1": 10
}
}
I want to filter out sets with enemy_id != 1. So a return would look like this:
{
"9": {
"enemy_id": 2,
"ai_": 5,
"ailmentcast1": 2
},
"2": {
"enemy_id": 4,
"ai_": 10,
"ailmentcast1": 7
},
"17": {
"enemy_id": 3,
"ai_": 7,
"ailmentcast1": 10
}
}
I have a functions I have tried but I'm having a hard time with the formatting.
function find_in_object(my_object, my_criteria){
return my_object.filter(function(obj) {
return Object.keys(my_criteria).every(function(c) {
return obj[c] == my_criteria[c];
});
});
}
And the code I ran that didn't work:
var newdata = [];
var filter = JSON.parse(content, function(key, value) {
if ( value.enemy_id !== 1 ) newdata.push(value);
return value; });
Any help would be greatly appreciated!
JSON.parseis a function that can clean up the whole object before it's returned. It looks like here you're presuming it gives you individual values, which it doesn't.