javascript find and remove object in array based on key value

Tom picture Tom · Feb 9, 2014 · Viewed 257.5k times · Source

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:

[
    {"id":"88","name":"Lets go testing"},
    {"id":"99","name":"Have fun boys and girls"},
    {"id":"108","name":"You are awesome!"}
]

I'm able to search the array using jQuery $grep;

var id = 88;

var result = $.grep(data, function(e){ 
     return e.id == id; 
});

But how can I delete the entire object when id == 88, and return data like this:

Data:

[
    {"id":"99","name":"Have fun boys and girls"},
    {"id":"108","name":"You are awesome!"}
]

Answer

Adam Boostani picture Adam Boostani · May 25, 2016

here is a solution if you are not using jquery:

myArray = myArray.filter(function( obj ) {
  return obj.id !== id;
});