Array.prototype.splice()

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

语法

¥Syntax

js
splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2)
splice(start, deleteCount, item1, item2, /* …, */ itemN)

参数

¥Parameters

start

开始更改数组的从零开始的索引,转换为整数

  • 负索引从数组末尾开始倒数 — 如果使用 -array.length <= start < 0start + array.length
  • 如果使用 start < -array.length0
  • 如果是 start >= array.length,则不会删除任何元素,但该方法将充当添加函数,添加所提供的尽可能多的元素。
  • 如果省略 start(并且不带参数调用 splice()),则不会删除任何内容。这与传递 undefined 不同,后者会转换为 0
deleteCount Optional

一个整数,指示数组中要从 start 中删除的元素数。

如果省略 deleteCount,或者其值大于或等于 start 指定位置之后的元素个数,则从 start 到数组末尾的所有元素都将被删除。但是,如果你希望传递任何 itemN 参数,则应将 Infinity 作为 deleteCount 传递,以删除 start 之后的所有元素,因为显式 undefined 会获取 converted0

如果 deleteCount0 或负数,则不会删除任何元素。在这种情况下,你应该至少指定一个新元素(见下文)。

item1, …, itemN Optional

要添加到数组的元素,从 start 开始。

如果不指定任何元素,splice() 只会从数组中删除元素。

返回值

¥Return value

包含已删除元素的数组。

¥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.

描述

¥Description

splice() 方法是 变异法 方法。它可能会改变 this 的内容。如果指定的要插入的元素数量与要删除的元素数量不同,则数组的 length 也会更改。同时,它使用 @@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 @@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.

示例

¥Examples

删除索引 2 之前的 0(零)个元素,并插入 "drum"

¥Remove 0 (zero) elements before index 2, and insert "drum"

js
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

删除索引 2 之前的 0(零)个元素,并插入 "drum" 和 "guitar"

¥Remove 0 (zero) elements before index 2, and insert "drum" and "guitar"

js
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().

js
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().

js
const myFish = ["angel", "clown", "mandarin"];
const removed = myFish.splice(myFish.length, 0, "sturgeon");

// myFish is ["angel", "clown", "mandarin", "sturgeon"]
// no items removed

删除索引 3 处的 1 个元素

¥Remove 1 element at index 3

js
const myFish = ["angel", "clown", "drum", "mandarin", "sturgeon"];
const removed = myFish.splice(3, 1);

// myFish is ["angel", "clown", "drum", "sturgeon"]
// removed is ["mandarin"]

删除索引 2 处的 1 个元素,并插入 "trumpet"

¥Remove 1 element at index 2, and insert "trumpet"

js
const myFish = ["angel", "clown", "drum", "sturgeon"];
const removed = myFish.splice(2, 1, "trumpet");

// myFish is ["angel", "clown", "trumpet", "sturgeon"]
// removed is ["drum"]

从索引 0 中删除 2 个元素,并插入 "parrot"、"anemone" 和 "blue"

¥Remove 2 elements from index 0, and insert "parrot", "anemone" and "blue"

js
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"]

从索引 2 开始删除 2 个元素

¥Remove 2 elements, starting from index 2

js
const myFish = ["parrot", "anemone", "blue", "trumpet", "sturgeon"];
const removed = myFish.splice(2, 2);

// myFish is ["parrot", "anemone", "sturgeon"]
// removed is ["blue", "trumpet"]

从索引 -2 中删除 1 个元素

¥Remove 1 element from index -2

js
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(-2, 1);

// myFish is ["angel", "clown", "sturgeon"]
// removed is ["mandarin"]

删除从索引 2 开始的所有元素

¥Remove all elements, starting from index 2

js
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2);

// myFish is ["angel", "clown"]
// removed is ["mandarin", "sturgeon"]

在稀疏数组上使用 splice()

¥Using splice() on sparse arrays

splice() 方法保留了数组的稀疏性。

¥The splice() method preserves the array's sparseness.

js
const arr = [1, , 3, 4, , 6];
console.log(arr.splice(1, 2)); // [empty, 3]
console.log(arr); // [1, 4, empty, 6]

对非数组对象调用 splice()

¥Calling splice() on non-array objects

splice() 方法读取 thislength 属性。然后,它根据需要更新整数键控属性和 length 属性。

¥The splice() method reads the length property of this. It then updates the integer-keyed properties and the length property as needed.

js
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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看