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

3

5

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

Plaudit answered 25/7, 2018 at 12:3 Comment(3)
Your for loop is looking at Arr.length which changes every time the loop iterates. The first time it iterates it checks if i (currently 0) is smaller than 10. Then you remove an item and increment i to 1. On the next iteration it checks if i (now 1) is smaller than 9. See what I'm getting at?Comyns
Not pretty sure i get it. But i get the pointPlaudit
Thanks for explaining bro! This gives me more to see whats happeningPlaudit
T
6

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;
Thicken answered 25/7, 2018 at 13:9 Comment(6)
Thank you very much, I'll try !Plaudit
Give me time 'till tomorrow. 'Cause i have to log out nowPlaudit
Array literals const array:Array = [] are considered to be idiomatic and also faster.Canonize
@FlorianSalihovic No and no. The [...] record is plainly shorter. Then, I just tested both with 1 million then 10 millions loops and, frankly, there's no much difference in elapsed time, the competition ends with +-50ms both ways. It is not impossible that there actually WAS difference at some point, but not with recent SDK/player versions.Thicken
@Thicken - Thanks for pointing that out. I would always refer to the sourceforge.net/adobe/flexsdk/wiki/Coding%20Conventions/…Canonize
@FlorianSalihovic It is most certainly good to have a scripting style, just don't venture into thinking that your style is the best. I looked through your link, some cases are fine with me, some others are... meh. The bad thing, they imperatively command do this, don't do that. I vouch for readable and easy-to-understand script, and also for straight architecture, but sometimes there are no time and sometimes an ugly solution is smaller and easier than refactoring a handful of classes, thus I compromise how to do things. Then I ALWAYS check myself what actually is faster.Thicken
M
4
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.

Mandorla answered 26/7, 2018 at 4:1 Comment(2)
Thank you very much! Those method are working the way i want. And now i know whats the real problem. Thank you everybody !Plaudit
Thank you very much! Those method are working the way i want. And now i know whats the real problem. Thank you everybody !Plaudit
D
1

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)

Distinguished answered 25/7, 2018 at 12:20 Comment(11)
I'll try it.Thank you friend!Plaudit
Give me time 'till tomorrow. 'Cause i have to log out nowPlaudit
No, delete wont work like that. With it you can only delete properties from array, not empty it.Chape
Whats the different between empty an array and only removing the properties of it? What is not removed when we remove the properties only?Plaudit
Whats the different between empty an array and only removing the properties of it? What is not removed when we remove the properties only?Plaudit
If your array.length is 5, after delete it's still 5. It might empty some values, but it wont empty the array. And his example will cause only errors, as it can not be used directly to the array itself.Chape
So you can for example delete Arr[0]; to empty the first property/value/element in it, but then the arrays length remains the same and Arr[0] will be undefined value.Chape
Ooh, now I got it.Thanks a lot @ChapePlaudit
@Kaarto I did not reply for loop. It is for remove entire elements without using for loopDistinguished
@SujathaGirijala no, it doesn't work like that. You can not delete fixed properties or variables with delete. Try it yourself or read here.Chape
@Chape thanks for your reply. Now I edited that answer please have a look on it. when you are freeDistinguished

© 2022 - 2024 — McMap. All rights reserved.