I have been trying several approaches on how to find an object in an array, where ID = var, and if found, remove the object from the array and return the new array of objects.
Data:
JavaScript
x
6
1
[
2
{"id":"88","name":"Lets go testing"},
3
{"id":"99","name":"Have fun boys and girls"},
4
{"id":"108","name":"You are awesome!"}
5
]
6
I’m able to search the array using jQuery $grep;
JavaScript
1
6
1
var id = 88;
2
3
var result = $.grep(data, function(e){
4
return e.id == id;
5
});
6
But how can I delete the entire object when id == 88, and return data like the following?
Data:
JavaScript
1
5
1
[
2
{"id":"99", "name":"Have fun boys and girls"},
3
{"id":"108", "name":"You are awesome!"}
4
]
5
Advertisement
Answer
I can grep the array for the id, but how can I delete the entire object where id == 88
Simply filter by the opposite predicate:
JavaScript
1
4
1
var data = $.grep(data, function(e){
2
return e.id != id;
3
});
4