as3 array remove by index

30,712

Solution 1

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);
}

Solution 2

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()

Share:
30,712
LeBlaireau
Author by

LeBlaireau

Updated on November 06, 2020

Comments

  • LeBlaireau
    LeBlaireau over 3 years

    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()
    }