How do I clear an array?

In flash, you only had to type “arrayname.length = 0”, but in Haxe, it’s get only, so how do I completely clear an array so that the length reads “0”?

Even less to type!

myArray = []


1 Like

That was simple, thanks!

You can also use openfl.Vector which has a length property :smile:

myArray = []

Careful. That do not clear the array. That creates a new one. In some cases that might be a problem (if you pass the array as argument of a function that is supposed to clear it for example. the array passed to the function would remain unchanged. That is only the inner function pointer that would point to the new empty array…)

For example this code:

var ta:Array<Int> = [1, 2, 3];

private static function myfunc(a:Array<Int>):Void
	a = [];

Would actually output:

and not

Fair point. Not sure i would ever use a function to clear or create a new array, but its certainly something important to point out. :+1:

the above example is rare, yes, but if you have more than 1 reference to the array, you’ll need to actually clear the array to have the changes reflected in every ref

while(arr.length > 0) arr.pop();

Instead of calling array.pop() a bunch of times, you can clear the array in one go using array.splice(0, array.length).