I have a case where I need to iterate through an array of objects, check a conditional on each element , delete existing object & add new object based on the outcome of the conditional. The code I have currently is as below, but of course it doesn't work.
What is the right approach to iterate through an array while adding / deleting new elements on certain iterations.
var arrayOfObjects = [] // a list to store objects of the same time
for(var i = 0; i < 5; i++){
arrayOfObjects.push(new someClass());
}
while(true){
for(var obj in arrayOfObjects){
// some conditional check on obj
// if check is true, delete the obj from array & add a new object
arrayOfObjects.splice(arrayOfObjects.indexOf(obj),1);
arrayOfObjects.push(new someClass());
}
}
while(true)you'll keep on repeating theforloop, over and over again... There is no stopping or breaking out of the while loop