The splice()
method changes the contents of an array by removing existing elements and/or adding new elements.
array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
start
deleteCount
Optional
deleteCount
is omitted, or if its value is larger than array.length - start
(that is, if it is greater than the number of elements left in the array, starting at start
), then all of the elements from start
through the end of the array will be deleted.deleteCount
is 0 or negative, no elements are removed. In this case, you should specify at least one new element (see below).item1, item2, ...
Optional
start
index. If you don't specify any elements, splice()
will only remove elements from the array.An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.
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.
var myFish = ['angel', 'clown', 'mandarin', 'sturgeon']; var removed = myFish.splice(2, 0, 'drum'); // myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"] // removed is [], no elements removed
var myFish = ['angel', 'clown', 'drum', 'mandarin', 'sturgeon']; var removed = myFish.splice(3, 1); // removed is ["mandarin"] // myFish is ["angel", "clown", "drum", "sturgeon"]
var myFish = ['angel', 'clown', 'drum', 'sturgeon']; var removed = myFish.splice(2, 1, 'trumpet'); // myFish is ["angel", "clown", "trumpet", "sturgeon"] // removed is ["drum"]
var myFish = ['angel', 'clown', 'trumpet', 'sturgeon']; var removed = myFish.splice(0, 2, 'parrot', 'anemone', 'blue'); // myFish is ["parrot", "anemone", "blue", "trumpet", "sturgeon"] // removed is ["angel", "clown"]
var myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon']; var removed = myFish.splice(myFish.length - 3, 2); // myFish is ["parrot", "anemone", "sturgeon"] // removed is ["blue", "trumpet"]
var myFish = ['angel', 'clown', 'mandarin', 'sturgeon']; var removed = myFish.splice(-2, 1); // myFish is ["angel", "clown", "sturgeon"] // removed is ["mandarin"]
var myFish = ['angel', 'clown', 'mandarin', 'sturgeon']; var removed = myFish.splice(2); // myFish is ["angel", "clown"] // removed is ["mandarin", "sturgeon"]
Specification | Status | Comment |
---|---|---|
ECMAScript 3rd Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.2. |
ECMAScript 5.1 (ECMA-262) The definition of 'Array.prototype.splice' in that specification. | Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Array.prototype.splice' in that specification. | Standard | |
ECMAScript Latest Draft (ECMA-262) The definition of 'Array.prototype.splice' in that specification. | Draft |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 1 | Yes | 1 | 5.5 | Yes | Yes |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | 4 | Yes | Yes | Yes |
Server | |
---|---|
Node.js | |
Basic support | Yes |
push()
/ pop()
— add/remove elements from the end of the arrayunshift()
/ shift()
— add/remove elements from the beginning of the arrayconcat()
— returns a new array comprised of this array joined with other array(s) and/or value(s)
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice