Skip to content

Instantly share code, notes, and snippets.

@scottopolis
Last active January 31, 2023 06:54
Show Gist options
  • Save scottopolis/6e35cf0d53bae81e6161662e6374da04 to your computer and use it in GitHub Desktop.
Save scottopolis/6e35cf0d53bae81e6161662e6374da04 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);
@KANE-99
Copy link

KANE-99 commented Mar 20, 2020

Also, Using findIndex is faster, so efficient way to

remove a single object from a array of objects

is,

apps.splice( apps.findIndex(a => a.id === 37) , 1);

@tamangsuresh
Copy link

Thank you all masters.

@skyzismo
Copy link

Exactly the thing I was looking for. Tks, bro.

@Spmoolman
Copy link

Awesome thanks

@KarameiOmid
Copy link

Thanks a lot, It was Great.

@ajingopi-bridge
Copy link

apps.splice( apps.findIndex(a => a.id === 37) , 1)

Super cool

Original solution also Awesome!!

@asayedio
Copy link

asayedio commented Nov 4, 2020

It was Great! saved a lot of time.

@jLeviner
Copy link

This is cool. Thank you.

@vsifiwe
Copy link

vsifiwe commented Nov 27, 2020

Been searching for a simple solution for hours. Thank you

@mrmgomes
Copy link

Thank you! I will be using this forever :-)

@BOVAGE
Copy link

BOVAGE commented Dec 22, 2020

Thanks. This will help me on my todo list delete function. have been searching for this

@marcelklein
Copy link

Many thanks

@pablomaccarone
Copy link

Great!

@ParvinEyvazov
Copy link

Thank you!

@PeterF2170
Copy link

+1

@blankcry
Copy link

blankcry commented Apr 8, 2021

Bless Up

@sagarsolanki1811
Copy link

apps = apps.filter(a => a.id !== 37);

It takes too much time and also contains extra space.
although perfectly fine !!!

@TomekQ13
Copy link

TomekQ13 commented Jun 25, 2021

Thanks!
It can be done easier now
apps.map(item => item.id).indexOf(37)

@mekitoci
Copy link

mekitoci commented Jul 9, 2021

thank u solve my problem!

@mojtabajahannia
Copy link

Thank you.

@runner855
Copy link

thank you exactly what I was looking for

@josevb1
Copy link

josevb1 commented Sep 30, 2021

Thanks!

@Aakash-Pacherkar
Copy link

Cool man thanks !!

@rebazomar121
Copy link

You save my time after 2 Hours of searching...Thank

@olivercrockett
Copy link

excellent, thanks!

@evinrgiuliani
Copy link

Awesome! Thank you.

@chmkmurali
Copy link

Thanks Buddy

@hemakumarm72
Copy link

thank you

@Khahory
Copy link

Khahory commented Nov 11, 2022

Ty

@Dharmadurai-D
Copy link

Very useful thankyou machi !

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