Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to remove all elements from an ActionScript Array?

I'm writing an application in Flex / ActionScript and have a number of class member variables of type Array storing data.

My question is: what's the "best" way to clear out an Array object?

I noticed the ArrayCollection class has a function removeAll() which does this, but the basic Array class does not. Some possibilities I've considered are:

  • Iterating through the array, calling pop or shift on each element
  • Setting the array length to 0
  • Setting the member variable to a "new Array()" or "[]"
like image 857
Chris R Avatar asked Sep 25 '09 18:09

Chris R


People also ask

Can you delete elements from an array?

Java arrays do not provide a direct remove method to remove an element. In fact, we have already discussed that arrays in Java are static so the size of the arrays cannot change once they are instantiated. Thus we cannot delete an element and reduce the array size.

How do you remove an element from an array in JavaScript?

Array elements can be deleted using the JavaScript operator delete . Using delete leaves undefined holes in the array. Use pop() or shift() instead.

Can you remove an element from an array C#?

We know that arrays in C# have fixed size, and we cannot simply remove an element from it. However, we can create a new array containing the desired elements from the original arrays.


1 Answers

I'd say:

myArray = [ ];

That's explicit, short, and makes good use of the VM's garbage collector.

Your first alternative runs a lot of interpreted code to get the same result.

I don't know that the second does what you want; if it does, it's hacky, unclear.

The "new Array()" variant of the third alternative is just wordy, offering no advantage over an array literal. If you also write JS and use JSLint, you'll get yelled at for not using the array literal form.

like image 116
Warren Young Avatar answered Oct 16 '22 09:10

Warren Young