+ 1
JS iterate over an array while eliminating or creating
I am using an array of objects to iterate and update various properties including eliminating colliding objects or adding new objects. I have read that in other languages like processing, resizing would lead to skip objects unless I use an iterator. I wanted to ask if my logic has this problem in line 175 and if so how to avoid it. Do I need to separate iteration from creation/ destruction? https://code.sololearn.com/WLckKJThtQ2V/?ref=app
4 Réponses
+ 2
👑Rajababu Shah👑 thank you for checking. I still hope to get an answer to my questions but this is helpful.
+ 1
WileyR Thanks! it was a while ago I asked this. I posted a code because I was asking about performance, not the answers I can google for documentation. I know what you write and also that one should travel the array backwards. this was giving heterogeneous performance on different platforms. What is difficult to find is good practice, not how can you do something but how should you do something
0
the way you delete an element from an array is to use array.splice(index, number)
arrays have a built in function to get the index of an object: array.indexOf(object)
so the final code is array.splice(array.indexOf(object), 1);