Home » javascript » Remove item from array of objects using grep jQuery

Remove item from array of objects using grep jQuery

Let say we have Array of object as given below using grep jQuery.

var data=[
{"id":"1","name":"Manoj"},
{"id":"2","name":"Suresh"},
{"id":"3","name":"Dipika"}
];

What to do remove object with id =1 from data ?
We use grep in jQuery as below.

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

here result is

[
{"id":"2","name":"Suresh"},
{"id":"3","name":"Dipika"}
]

Note: Finds the elements of an array which satisfy a filter function. The original array is not affected.

Happy Coding with filter on jQuery 🙂

4 thoughts on “Remove item from array of objects using grep jQuery

  1. or you can do as :
    var id = 1;
    var result = $.grep(data, function(e){
    return e.id == id;
    }, true);

    Happy coding 🙂

    1. Manoj says:

      Hello Suresh,
      Thank you for your reply.

  2. Aashish says:

    This is very interesting read. A nice way to remove item from array of objects. Great guys!

Leave a Reply

Subscribe

Join 901 other subscribers