If I add or delete an element while iterating over an Array or an Array-like object in Javascript, do I need to take into account that the underlying collection may have changed or that the iterators may have become invalidated? In other words, is there an equivalent to the Erase–remove idiom for C++ in Javascript?
Asked
Active
Viewed 116 times
1
-
1Usually, JavaScript is used in a functional fashion. It means that you are likely to use the `filter` method before `map` or `forEach` – Christian Vincenzo Traina Oct 05 '20 at 10:11
-
1By the way, yes you may need to take into account that your "next element" isn't what you expect, if you modify the array while iterating. But this doesn't happen in functional programming since it's everything immutable. – Christian Vincenzo Traina Oct 05 '20 at 10:15
-
I think these two comments answer my question. If you post an answer, I'll mark it as accepted. – mallwright Oct 05 '20 at 10:25
1 Answers
0
There's no equivalent because JS has garbage collection, i.e., you don't need to call erase
on the tail after calling the equivalent of remove.
You can delete from an array while iterating.

user835611
- 2,309
- 2
- 21
- 29