When you remove elements from an array in a for statement, you need to change the value of the index variable accordingly
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var numbers:Array = new Array(4, 10);
numbers[4] = 1;
trace(numbers); // Displays: 4,10,undefined,undefined,1
for(var i:int = 0; i < numbers.length; i++) {
if(numbers[i] == undefined) {
numbers.splice(i, 1);
}
}
trace(numbers); // Displays: 4,10,undefined,1
}
}
}
// 4,10,,,1
// 4,10,,1
Related examples in the same category