splice (JavaScript)

Deletes elements from an array, optionally replacing them, to form another array.

Defined in

Array (Standard - JavaScript)

Syntax

splice(start:int, deletecount:int, ...) : Array (Standard - JavaScript)
Parameters Description
start Starting element, inclusive, where 0 is the first element.
deletecount Number of elements to delete.
... A comma-separated list of replacement elements. The number of replacement elements can differ from the number of deleted elements.

Usage

If 0 elements are deleted and replacement parameters are specified, the operation is essentially an insertion at the beginning of the array.
Return value Description
Array Elements excluding the deleted ones and including any replacement elements.

Examples

(1) This computed label deletes the first two elements of an array.
var a = new Array("one", "two", "three", "four");
a.splice(0, 2) // [three, four]
(2) This computed label replaces the first two elements of an array.
var a = new Array("one", "two", "three", "four");
a.splice(0, 2, "alpha", "beta") // [alpha, beta, three, four]
(3) This computed label inserts elements at the start of an array.
var a = new Array("one", "two", "three", "four");
a.splice(0, 0, "alpha", "beta") // [alpha, beta, one, two, three, four]