0

I have this list of objects:

mylistobjects = [{id: 1, name: 'One'}, {id: 2, name: 'Two'}, {id: 3, name: 'Three'}, {id: 4, name: 'Four'}, {id: 5, name: 'Five'}]

How can I find and remove object from the list by its id ?

faoxis
  • 1,912
  • 5
  • 16
  • 31
  • use `filter()` to filter out elements from it .... or iterate and remove elements from existing array using `splice()` – Pranav C Balan Jan 18 '17 at 04:39
  • By filtering the array with a condition where the `id` of the element is one you want to keep. –  Jan 18 '17 at 04:40
  • @PranavCBalan Filter **can** be used here, but there are more simple ways to do this (for loop and find id) – yummypasta Jan 18 '17 at 04:40
  • 2
    The dupe target can be found as the second result in Google when searching “javascript remove object by property”. – Sebastian Simon Jan 18 '17 at 04:41
  • 1
    @yummypasta Why is that simpler? After you find the ID, how are you planning to remove it? How could it be simpler than `mylistobjects.filter(obj => obj.id !== badId)`? –  Jan 18 '17 at 04:41
  • See also http://stackoverflow.com/questions/2722159/javascript-how-to-filter-object-array-based-on-attributes. –  Jan 18 '17 at 04:42
  • 1
    @yummypasta : not just find, he wants to remove it ..... even I'd added both ways in my comment – Pranav C Balan Jan 18 '17 at 04:44
  • By the way, in JavaScript lists are called "arrays". –  Jan 18 '17 at 04:46

0 Answers0