Isabel
0
Q:

how to find and remove object from array in javascript

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' }
// [
1
 // we have an array of objects, we want to remove one object using only the id property
var apps = [{id:34,name:'My App',another:'thing'},{id:37,name:'My New App',another:'things'}];
 
// get index of object with id:37
var removeIndex = apps.map(function(item) { return item.id; }).indexOf(37);
 
// remove object
apps.splice(removeIndex, 1);
1
var id = 88;

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

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

New to Communities?

Join the community