Understanding Array::splice in ActionScript 3
Asked Answered
T

4

11

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.

Trotter answered 12/1, 2010 at 1:48 Comment(1)
I'm also not happy about array.spliceOwlish
C
40

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

Concepcion answered 12/1, 2010 at 1:50 Comment(0)
F
7

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

array.splice(i,1);
Fullerton answered 12/1, 2010 at 1:54 Comment(0)
B
4

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.

Boylston answered 23/2, 2012 at 20:24 Comment(0)
C
2

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 );
Colophon answered 3/10, 2013 at 20:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.