Array.prototype.splice()

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

splice() 方法用于更改数组的内容,通过删除或替换现有元素,以及/或者 原地添加新元素。

要创建一个新数组,移除和/或替换其中一部分元素而不修改原始数组,请使用 toSpliced()。要访问数组的一部分而不修改它,请参阅 slice()

试一试

const months = ["Jan", "March", "April", "June"];
months.splice(1, 0, "Feb");
// Inserts at index 1
console.log(months);
// Expected output: Array ["Jan", "Feb", "March", "April", "June"]

months.splice(4, 1, "May");
// Replaces 1 element at index 4
console.log(months);
// Expected output: Array ["Jan", "Feb", "March", "April", "May"]

语法

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

参数

start

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

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

一个整数,指示要从 start 位置开始从数组中移除的元素数量。

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

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

item1, …, itemN 可选

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

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

返回值

包含已删除元素的数组。

如果只移除一个元素,则返回包含一个元素的数组。

如果没有移除任何元素,则返回一个空数组。

描述

splice() 方法是变异方法。它可能会更改 this 的内容。如果指定的插入元素数量与移除的元素数量不同,数组的 length 也会被更改。同时,它使用 [Symbol.species] 来创建要返回的新数组实例。

如果删除的部分是稀疏的,则 splice() 返回的数组也是稀疏的,具有相应的空槽索引。

splice() 方法是通用的。它只期望 this 值具有 length 属性和整数键属性。尽管字符串也具有类数组的特性,但该方法不适合应用于字符串,因为字符串是不可变的。

示例

在索引 2 之前移除 0 (零) 个元素,并插入 "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"

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"

splice(0, 0, ...elements) 就像 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"

splice(array.length, 0, ...elements) 就像 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 个元素

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"

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"

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 个元素

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 个元素

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

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

从索引 2 开始移除所有元素

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

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

在稀疏数组上使用 splice()

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

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

在非数组对象上调用 splice()

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

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' }

规范

规范
ECMAScript® 2026 语言规范
# sec-array.prototype.splice

浏览器兼容性

另见