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.

Adam McKenna
  • 2,295
  • 6
  • 30
  • 41
numerical25
  • 10,524
  • 36
  • 130
  • 209
  • I'm also not happy about [`array.splice`](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#splice()) – n4pgamer Oct 31 '13 at 15:01

4 Answers4

40

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

Anon.
  • 58,739
  • 8
  • 81
  • 86
7

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

array.splice(i,1);
Moshe
  • 57,511
  • 78
  • 272
  • 425
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.

Shashi
  • 41
  • 1
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 );
Bucket
  • 7,415
  • 9
  • 35
  • 45