Array.prototype.splice()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Array 实例的 splice() 方法通过删除或替换现有元素和/或添加新元素 到位 来更改数组的内容。
¥The splice() method of Array instances changes the contents of an array by
removing or replacing existing elements and/or adding new elements in place.
要创建删除和/或替换段的新数组而不改变原始数组,请使用 toSpliced()。要访问数组的一部分而不修改它,请参阅 slice()。
¥To create a new array with a segment removed and/or replaced without mutating the original array, use toSpliced(). To access part of an array without modifying it, see slice().
Try it
语法
参数
¥Parameters
start-
开始更改数组的从零开始的索引,转换为整数。
- 负索引从数组末尾开始倒数 — 如果使用
-array.length <= start < 0、start + array.length。 - 如果使用
start < -array.length、0。 - 如果是
start >= array.length,则不会删除任何元素,但该方法将充当添加函数,添加所提供的尽可能多的元素。 - 如果省略
start(并且不带参数调用splice()),则不会删除任何内容。这与传递undefined不同,后者会转换为0。
- 负索引从数组末尾开始倒数 — 如果使用
deleteCountOptional-
一个整数,指示数组中要从
start中删除的元素数。如果省略
deleteCount,或者其值大于或等于start指定位置之后的元素个数,则从start到数组末尾的所有元素都将被删除。但是,如果你希望传递任何itemN参数,则应将Infinity作为deleteCount传递,以删除start之后的所有元素,因为显式undefined会获取 converted 到0。如果
deleteCount为0或负数,则不会删除任何元素。在这种情况下,你应该至少指定一个新元素(见下文)。 item1, …,itemNOptional-
要添加到数组的元素,从
start开始。如果不指定任何元素,
splice()只会从数组中删除元素。
返回值
描述
¥Description
splice() 方法是 变异法 方法。它可能会改变 this 的内容。如果指定的要插入的元素数量与要删除的元素数量不同,则数组的 length 也会更改。同时,它使用 [Symbol.species] 创建一个要返回的新数组实例。
¥The splice() method is a mutating method. It may change the content of this. If the specified number of elements to insert differs from the number of elements being removed, the array's length will be changed as well. At the same time, it uses [Symbol.species] to create a new array instance to be returned.
如果删除的部分是 sparse,则 splice() 返回的数组也是稀疏的,相应的索引为空槽。
¥If the deleted portion is sparse, the array returned by splice() is sparse as well, with those corresponding indices being empty slots.
splice() 方法是 generic。它只期望 this 值具有 length 属性和整数键控属性。虽然字符串也是类似数组的,但此方法不适合应用于它们,因为字符串是不可变的。
¥The splice() method is generic. It only expects the this value to have a length property and integer-keyed properties. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.
示例
删除索引 2 之前的 0(零)个元素,并插入 "drum"
删除索引 2 之前的 0(零)个元素,并插入 "drum" 和 "guitar"
¥Remove 0 (zero) elements before index 2, and insert "drum" and "guitar"
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
删除索引 0 处的 0(零)个元素,并插入 "angel"
¥Remove 0 (zero) elements at index 0, and insert "angel"
splice(0, 0, ...elements) 像 unshift() 一样在数组的开头插入元素。
¥splice(0, 0, ...elements) inserts elements at the start of the array like unshift().
const myFish = ["clown", "mandarin", "sturgeon"];
const removed = myFish.splice(0, 0, "angel");
// myFish is ["angel", "clown", "mandarin", "sturgeon"]
// no items removed
删除最后一个索引处的 0(零)个元素,并插入 "sturgeon"
¥Remove 0 (zero) elements at last index, and insert "sturgeon"
splice(array.length, 0, ...elements) 像 push() 一样在数组末尾插入元素。
¥splice(array.length, 0, ...elements) inserts elements at the end of the array like push().
const myFish = ["angel", "clown", "mandarin"];
const removed = myFish.splice(myFish.length, 0, "sturgeon");
// myFish is ["angel", "clown", "mandarin", "sturgeon"]
// no items removed
删除索引 3 处的 1 个元素
删除索引 2 处的 1 个元素,并插入 "trumpet"
从索引 0 中删除 2 个元素,并插入 "parrot"、"anemone" 和 "blue"
从索引 2 开始删除 2 个元素
从索引 -2 中删除 1 个元素
删除从索引 2 开始的所有元素
在稀疏数组上使用 splice()
对非数组对象调用 splice()
¥Calling splice() on non-array objects
splice() 方法读取 this 的 length 属性。然后,它根据需要更新整数键控属性和 length 属性。
¥The splice() method reads the length property of this. It then updates the integer-keyed properties and the length property as needed.
const arrayLike = {
length: 3,
unrelated: "foo",
0: 5,
2: 4,
};
console.log(Array.prototype.splice.call(arrayLike, 0, 1, 2, 3));
// [ 5 ]
console.log(arrayLike);
// { '0': 2, '1': 3, '3': 4, length: 4, unrelated: 'foo' }
规范
| Specification |
|---|
| ECMAScript Language Specification # sec-array.prototype.splice |
浏览器兼容性
BCD tables only load in the browser