javascript array find and remove object code example
Example 1: locate and delete an object in an array
var apps = [{id:34,name:'My App',another:'thing'},{id:37,name:'My New App',another:'things'}];
var removeIndex = apps.map(function(item) { return item.id; }).indexOf(37);
apps.splice(removeIndex, 1);
Example 2: how to find and remove object from array in javascript
var id = 88;
for(var i = 0; i < data.length; i++) {
if(data[i].id == id) {
data.splice(i, 1);
break;
}
}
Example 3: locate and delete an object in an array
var people = [
{name: 'Billy', age: 22},
{name: 'Sally', age: 19},
{name: 'Timmy', age: 29},
{name: 'Tammy', age: 15}
];
_.remove(people, function(e) {
return e.age < 21
});
console.log(people);