Changes the content of an array, adding new elements while removing old elements.
Array
splice(index, howMany, newElt1, ..., newEltN)
index
Index at which to start changing the array.
howMany
An integer indicating the number of old array elements to remove. If howMany is 0, no elements are removed. In this case, you should specify at least one new element.
newElt1...newEltN
(Optional) The elements to add to the array. If you don't specify any elements, splice simply removes elements from the array.
If you specify a different number of elements to insert than the number you're removing, the array will have a different length at the end of the call. If howMany is 1, this method returns the single element that it removes. If howMany is more than 1, the method returns an array containing the removed elements.
The following script illustrates the use of the splice:
myFish = ["angel", "clown", "mandarin", "surgeon"]; Console.Write("myFish: " + myFish); removed = myFish.splice(2, 0, "drum"); Console.Write("After adding 1: " + myFish); Console.Write("removed is: " + removed); removed = myFish.splice(3, 1) Console.Write("After removing 1: " + myFish); Console.Write("removed is: " + removed); removed = myFish.splice(2, 1, "trumpet") Console.Write("After replacing 1: " + myFish); Console.Write("removed is: " + removed); removed = myFish.splice(0, 2, "parrot", "anemone", "blue") Console.Write("After replacing 2: " + myFish); Console.Write("removed is: " + removed);
myFish: ["angel", "clown", "mandarin", "surgeon"] After adding 1: ["angel", "clown", "drum", "mandarin", "surgeon"] removed is: undefined After removing 1: ["angel", "clown", "drum", "surgeon"] removed is: mandarin After replacing 1: ["angel", "clown", "trumpet", "surgeon"] removed is: drum After replacing 2: ["parrot", "anemone", "blue", "trumpet", "surgeon"] removed is: ["angel", "clown"]