Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

as3 array remove by index

I have an array 'cat', 'dog', 'budgie'

and want to remove the item by index.

At the moment I have

function removeit(myindex) {
    animals[myindex] = animals.pop()
}
like image 273
LeBlaireau Avatar asked Apr 10 '12 13:04

LeBlaireau


People also ask

How do you delete an element from an array?

You can remove the element at any index by using the splice method. If you have an array named arr it can be used in this way to remove an element at any index: arr. splice(n, 1) , with n being the index of the element to remove.

How do you remove an array from a name?

The correct way to remove an item from an array is to use splice() . It takes an index and amount of items to delete starting from that index.

What is array in action script?

Actionscript Course Arrays are variables that store multiple values in a single variable name. So, an Array is a variable with an ordered list of values.


2 Answers

You want splice

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#splice%28%29

Array.splice(starting point, remove count);

 var newArray:Array = myArray.splice(2, 1); //this removes whatever is at index 2 and returns it in a new array.

Change your function to

function removeit(myindex) {
    animals.splice(myindex, 1);
}
like image 143
francis Avatar answered Sep 28 '22 05:09

francis


When using Flash Player 19+ or AIR 19+, you can use

myArray.removeAt(myIndex); // removes the element at the specified index, modifying the array without making a copy

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#removeAt()

like image 29
low-belly Avatar answered Sep 28 '22 05:09

low-belly