how to delete an object in array javascript code example
Example 1: javascript remove object from array
const array = [
{ id: 1, name: 'serdar' },
{ id: 5, name: 'alex' },
{ id: 300, name: 'brittany' }
];
const idToRemove = 5;
const filterArray = array.filter((item) => item.id !== idToRemove); //ES6
// [
// { id: 1, name: 'serdar' },
// { id: 300, name: 'brittany' }
// [
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;
}
}