Skip to content

Instantly share code, notes, and snippets.

@hrishi7
Forked from scottopolis/splice-object-array.js
Created November 22, 2021 11:45
Show Gist options
  • Save hrishi7/6349b6433c74c1260a46665703096d0c to your computer and use it in GitHub Desktop.
Save hrishi7/6349b6433c74c1260a46665703096d0c to your computer and use it in GitHub Desktop.
Remove object from array of objects in Javascript
// we have an array of objects, we want to remove one object using only the id property
const apps = [{id:34,name:'My App',another:'thing'},{id:37,name:'My New App',another:'things'}];
// get index of object with id of 37
const removeIndex = apps.findIndex( item => item.id === 37 );
// remove object
apps.splice( removeIndex, 1 );
/*** Code above updated 7/21 with findIndex, a much faster method. Old code below for reference. ***/
// source: http://stackoverflow.com/questions/16491758/remove-objects-from-array-by-object-property
// get index of object with id:37
var removeIndex = apps.map(function(item) { return item.id; }).indexOf(37);
// remove object
apps.splice(removeIndex, 1);
@hrishi7
Copy link
Author

hrishi7 commented Nov 22, 2021

sas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment