Understanding Array::splice in ActionScript 3

45,113

Solution 1

If you want to remove one element, you call splice(index, 1).

Solution 2

Your code will delete zero things is what you are describing. Change the second parameter to a 1:

array.splice(i,1);

Solution 3

We can do two thing with splice method.

  1. To delete the first element from array. arrayName.splice(index,no of element)

    i.e myArr.splice(0,1); //it's delete first element from array

    Note: Array index start from 0,1,2 and so on....

  2. To add element into array. arrayName.splice(index to add,0,elem1,elem2) i.e. myArr.splice(0,0,"A","B"); Note:it add A,B into myArr start from zero position and shift the existing element's index no.

Solution 4

The best way to remove the first item from an array is using shift()

myArray.shift();

You can add an item on the beginning of the array too using unshift().

myArray.unshift( item );
Share:
45,113
numerical25
Author by

numerical25

Updated on July 09, 2022

Comments

  • numerical25
    numerical25 almost 2 years

    I am trying to remove an object from an array, but for some reason it's not working. I am under the impression that a splice accepts 2 parameters: first, the position in the array to begin at. And for parameter 2, how many to delete from then on out.

    I just want to delete one entry so I am doing this:

    array.splice(i,0);
    

    But it isn't working. Can someone tell me what I am doing wrong and enlighten me on how it is supposed to work.