JS iterate over an array while eliminating or creating | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

17th Jul 2020, 2:10 PM
bell
bell - avatar
3 Answers
+ 2
👑Rajababu Shah👑 thank you for checking. I still hope to get an answer to my questions but this is helpful.
21st Jul 2020, 5:44 PM
bell
bell - avatar
+ 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
20th Feb 2021, 1:59 PM
bell
bell - avatar
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);
20th Feb 2021, 1:06 PM
WileyR
WileyR - avatar