E
E
Erling2016-06-23 13:19:50
JavaScript
Erling, 2016-06-23 13:19:50

How to remove an element from an array if you know its ID?

There is an array of objects, each of which has ID properties (starting from one). What is the best way to remove an element from an array by its id?
PS it's about angular + fullcalendar.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Melnikov, 2016-06-23
@Erling

var someArray = [{id: 1}, {id: 2}, {id: 3}],
  idToDelete = 2
someArray.forEach(function(el, i) {
  if (el.id == idToDelete) someArray.splice(i, 1)
})
console.log(someArray)
// [{id: 1}, {id: 3}]

Y
Yuri Esin, 2016-06-23
@Exomode


On pure JavaScript, it is enough to do this: they are logically always unique and the memory consumption for storing data is less.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question