splice()
method changes the contents of an array by removing or replacing existing elements and/or adding new elements
in place
.
let arrDeletedItems = array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
start
The index at which to start changing 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 element as item[n*] provided.
-1
, meaning
-
n
is the index of the
n
th
last element, and is therefore equivalent to the index of
array
.length -
n
.) If
array
.length +
start
is less than
0
, it will begin from index
0
.
deleteCount
可选
start
.
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.
注意:
In IE8, it won't delete all when
deleteCount
is omitted.
deleteCount
is
0
or negative, no elements are removed. In this case, you should specify at least one new element (see below).
item1
,
item2
, ...
可选
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.
let myFish = ['angel', 'clown', 'mandarin', 'sturgeon'] let removed = myFish.splice(2, 0, 'drum') // myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"] // removed is [], no elements removed
let myFish = ['angel', 'clown', 'mandarin', 'sturgeon'] let removed = myFish.splice(2, 0, 'drum', 'guitar') // myFish is ["angel", "clown", "drum", "guitar", "mandarin", "sturgeon"] // removed is [], no elements removed
let myFish = ['angel', 'clown', 'drum', 'mandarin', 'sturgeon'] let removed = myFish.splice(3, 1) // myFish is ["angel", "clown", "drum", "sturgeon"] // removed is ["mandarin"]
let myFish = ['angel', 'clown', 'drum', 'sturgeon'] let removed = myFish.splice(2, 1, 'trumpet') // myFish is ["angel", "clown", "trumpet", "sturgeon"] // removed is ["drum"]
let myFish = ['angel', 'clown', 'trumpet', 'sturgeon'] let removed = myFish.splice(0, 2, 'parrot', 'anemone', 'blue') // myFish is ["parrot", "anemone", "blue", "trumpet", "sturgeon"] // removed is ["angel", "clown"]
let myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon'] let removed = myFish.splice(2, 2) // myFish is ["parrot", "anemone", "sturgeon"] // removed is ["blue", "trumpet"]
let myFish = ['angel', 'clown', 'mandarin', 'sturgeon'] let removed = myFish.splice(-2, 1) // myFish is ["angel", "clown", "sturgeon"] // removed is ["mandarin"]
let myFish = ['angel', 'clown', 'mandarin', 'sturgeon'] let removed = myFish.splice(2) // myFish is ["angel", "clown"] // removed is ["mandarin", "sturgeon"]
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Array.prototype.splice' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
splice
|
Chrome 1 | Edge 12 | Firefox 1 |
IE
5.5
|
Opera 4 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
完整支持
见实现注意事项。
push()
/
pop()
— add/remove elements from the end of the array
unshift()
/
shift()
— add/remove elements from the beginning of the array
concat()
— returns a new array comprised of this array joined with other array(s) and/or value(s)
Array
Array.from()
Array.isArray()
Array.of()
Array.prototype.concat()
Array.prototype.copyWithin()
Array.prototype.entries()
Array.prototype.every()
Array.prototype.fill()
Array.prototype.filter()
Array.prototype.find()
Array.prototype.findIndex()
Array.prototype.flat()
Array.prototype.flatMap()
Array.prototype.forEach()
Array.prototype.includes()
Array.prototype.indexOf()
Array.prototype.join()
Array.prototype.keys()
Array.prototype.lastIndexOf()
Array.prototype.map()
Array.prototype.pop()
Array.prototype.push()
Array.prototype.reduce()
Array.prototype.reduceRight()
Array.prototype.reverse()
Array.prototype.shift()
Array.prototype.slice()
Array.prototype.some()
Array.prototype.sort()
Array.prototype.splice()
Array.prototype.toLocaleString()
Array.prototype.toSource()
Array.prototype.toString()
Array.prototype.unshift()
Array.prototype.values()
Array.prototype[@@iterator]()
get Array[@@species]
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()