The splice()
method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. To access part of an array without modifying it, see slice()
.
The splice()
method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. To access part of an array without modifying it, see slice()
.
splice(start) splice(start, deleteCount) splice(start, deleteCount, item1) splice(start, deleteCount, item1, item2, itemN)
start
The index at which to start changing the array.
If greater than the length of the array, start
will be set to the length of the array. In this case, no element will be deleted but the method will behave as an adding function, adding as many elements as items provided.
If negative, it will begin that many elements from the end of the array. (In this case, the origin -1
, meaning -n
is the index of the n
th last element, and is therefore equivalent to the index of array.length - n
.) If start
is -Infinity
, it will begin from index 0
.
deleteCount
Optional
An integer indicating the number of elements in the array to remove from start
.
If deleteCount
is omitted, or if its value is equal to or larger than array.length - start
(that is, if it is equal to or greater than the number of elements left in the array, starting at start
), then all the elements from start
to the end of the array will be deleted. However, it must not be omitted if there is any item1
parameter.
If deleteCount
is 0
or negative, no elements are removed. In this case, you should specify at least one new element (see below).
item1
, …, itemN
Optional
The elements to add to the array, beginning from start
.
If you do not 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 the specified number of elements to insert differs from the number of elements being removed, the array's length
will be changed.
const myFish = ['angel', 'clown', 'mandarin', 'sturgeon']; const removed = myFish.splice(2, 0, 'drum'); // myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"] // removed is [], no elements removed
const myFish = ['angel', 'clown', 'mandarin', 'sturgeon']; const removed = myFish.splice(2, 0, 'drum', 'guitar'); // myFish is ["angel", "clown", "drum", "guitar", "mandarin", "sturgeon"] // removed is [], no elements removed
const myFish = ['angel', 'clown', 'drum', 'mandarin', 'sturgeon']; const removed = myFish.splice(3, 1); // myFish is ["angel", "clown", "drum", "sturgeon"] // removed is ["mandarin"]
const myFish = ['angel', 'clown', 'drum', 'sturgeon']; const removed = myFish.splice(2, 1, 'trumpet'); // myFish is ["angel", "clown", "trumpet", "sturgeon"] // removed is ["drum"]
const myFish = ['angel', 'clown', 'trumpet', 'sturgeon']; const removed = myFish.splice(0, 2, 'parrot', 'anemone', 'blue'); // myFish is ["parrot", "anemone", "blue", "trumpet", "sturgeon"] // removed is ["angel", "clown"]
const myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon']; const removed = myFish.splice(2, 2); // myFish is ["parrot", "anemone", "sturgeon"] // removed is ["blue", "trumpet"]
const myFish = ['angel', 'clown', 'mandarin', 'sturgeon']; const removed = myFish.splice(-2, 1); // myFish is ["angel", "clown", "sturgeon"] // removed is ["mandarin"]
const myFish = ['angel', 'clown', 'mandarin', 'sturgeon']; const removed = myFish.splice(2); // myFish is ["angel", "clown"] // removed is ["mandarin", "sturgeon"]
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
splice |
1 |
12 |
1 |
5.5
From Internet Explorer 5.5 through 8, all elements of the array will not be deleted if
deleteCount is omitted. This behavior was fixed in Internet Explorer 9. |
4 |
1 |
4.4 |
18 |
4 |
10.1 |
1 |
1.0 |
1.0 |
0.10.0 |
© 2005–2022 MDN 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