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()
}
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.
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.
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.
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);
}
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With