Array.splice

Changes the content of an array, adding new elements while removing old elements.
Method of Array

Syntax

array.splice( index, howMany, [element1][, ..., elementN] )

Parameters

Parameter Description
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.
element1, ..., element_N The elements to add to the array. If you don't specify any elements, splice simply removes elements from the array.

Description

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.
The splice method returns an array containing the removed elements. If only one element is removed, an array of one element is returned.

Examples

Example 1: The following script illustrates the use of splice without removal of array elements:

This example produces the following results:

myFish is: ["angel", "clown", "drum", "mandarin", "sturgeon"]
removed is: undefined

Example 2: The following script illustrates the use of splice with removal of array elements:

This example produces the following results:
myFish is: ["angel", "clown", "drum", "sturgeon"]
removed is: ["mandarin"]

Example 3: Replace elements of array with elements of another array:

This example produces the following results:
a is: [ 2, 3 ]
b is: [ 2, 3, 4 ]

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.