Yudhis
Yudhis

Reputation: 53

AS3:removing every elements in an array at once using for loop

EDIT: EVERY ANSWER BELOW ARE WORKING, THANKS FOR HELPING ME!

I'm currently learn about splicing an array in as3. So here's my code:

//import classes
import flash.utils.Timer;
import flash.events.*;

//variables
var Arr:Array=new Array();
var num:Number=0;
//set a timer and set timer limit of 10 times
var timer:Timer=new Timer(1000,10);

//add a listener to our timer object
timer.addEventListener(TimerEvent.TIMER, tick);
timer.addEventListener(TimerEvent.TIMER_COMPLETE,tock);

//tick function
function tick(e:TimerEvent):void{
    //i add an element each time the timer 'ticks'
    Arr.push(['index'+num]);
    num++;
}
//tock function
function tock(e:TimerEvent):void{
trace('array elements :'+Arr);//traces Arr elemnts
for(var i:int=0;i<Arr.length;i++){
    Arr.splice(i,1);// i've tried Arr.splice(0,1), but neither working
    trace('elemnts left : '+Arr);
}

I dont really understand the problem, but here's the result:

1.not every elements in Arr array have been removed 2.the maximum Arr's length is ten before spliced.BUT in the loop, its only splicing less than ten times, which it causes the problem above

anybody have an idea for this? Please help me out

Upvotes: 5

Views: 567

Answers (3)

Sujatha Girijala
Sujatha Girijala

Reputation: 1209

you can write in this way. It will remove the entire elements

 function tock(e:TimerEvent):void{ 

     var i = Arr.length
    while (i--) {
        ...
        if (...) { 
            Arr.splice(i, 1);
        } 
    }    
}

otherwise you just reinitialize that array(Arr)

Upvotes: 1

user1234567
user1234567

Reputation: 333

for(var i:int=0;i<Arr.length;i++)

This is why it doesn't splice everything. Every time this loop runs, Arr.length is decreased by 1 since you spliced it, so once it gets to i==5(sixth loop), the conditions become fulfilled as 'i'(5) is no longer less than Arr.length(5 left in array), thus the loop stops.

You conditions should be to splice as long as the array has more than 0 items. Try this instead:

for(var i:int=0;Arr.length>0;i++)

Also, splice works like this. Arr.splice(INDEX, AMOUNT TO REMOVE). In this case you can splice at index0 to remove them one by one from the bottom. thus the right way to write this is:

Arr.splice(0,1)

If your goal is just to empty the array, simply do

Arr.length = 0;

On a side note, you dont need to put those square brackets when pushing a new array,

Arr.push('index '+num);

works just as well.

Upvotes: 4

Organis
Organis

Reputation: 7316

There are simpler and faster options:

You can just set the Array's length to 0. That will effectively remove all its elements at once.

Arr.length = 0;

You can create a new empty instance of the Array class. That will not destroy the original object immediately, but if there are no references to it, it will be consumed by the Garbage Collector eventually, so you won't need to think of it.

// You can omit () with the "new" operator if there are no mandatory arguments.
Arr = new Array;

Upvotes: 6

Related Questions