javascript find and remove object in array based on key value javascript find and remove object in array based on key value arrays arrays

javascript find and remove object in array based on key value


here is a solution if you are not using jquery:

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


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:

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


You can simplify this, and there's really no need for using jquery here.

var id = 88;for(var i = 0; i < data.length; i++) {    if(data[i].id == id) {        data.splice(i, 1);        break;    }}

Just iterate through the list, find the matching id, splice, and then break to exit your loop