# Loop based on array length, splice question

**URL:** https://forum.kirupa.com/t/loop-based-on-array-length-splice-question/318976
**Category:** Uncategorized
**Created:** [February 5, 2011, 3:28am UTC](https://forum.kirupa.com/t/loop-based-on-array-length-splice-question/318976 "2011-02-05T03:28:29Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![ScarsOfDracula](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@ScarsOfDracula](https://forum.kirupa.com/u/ScarsOfDracula)
#### Post date: [February 5, 2011, 3:28am UTC](https://forum.kirupa.com/t/loop-based-on-array-length-splice-question/318976/1 "2011-02-05T03:28:29Z")

</div>

Not really limited to AS3.

I have an array with many values pushed into it (happen to be names of objects). I am using a for loop to check all of the objects (by index number) for collisions, etc. Periodically, depending on a condition, I want to remove the currently checked object’s name from the array list. I noticed that there is a problem if I immediately splice the checked name from the list. Namely, the list gets shorter, which is what I want, but not until I’m done checking all objects. Is it a good idea to solve the issue by incrementing i-- after the splice? I do this so that the object that used to be next in the list doesn’t get skipped.

It seems to work very well but I want to know if this is a lame/problematic solution and if there are any better ways of dealing with it. Thanks!

Here’s an example checked on enterFrame:

for (i=0;i\<myarray.length;i++){  
var ob=myarray\*  
//any old condition  
if (ob.x\>800){  
removeChild(ob)  
myarray.splice(i,1)  
//to avoid anyone being overlooked  
i–  
}  
}
