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 🙂
or you can do as :
var id = 1;
var result = $.grep(data, function(e){
return e.id == id;
}, true);
Happy coding 🙂
Hello Suresh,
Thank you for your reply.
This is very interesting read. A nice way to remove item from array of objects. Great guys!
Thank you Aashish