Array.prototype.splice()
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"]
语法
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。如果
deleteCount为0或负数,则不删除任何元素。在这种情况下,您应该指定至少一个新元素(见下文)。 item1, …,itemN可选-
要添加到数组中的元素,从
start位置开始。如果不指定任何元素,
splice()将仅从数组中删除元素。
返回值
包含已删除元素的数组。
如果只移除一个元素,则返回包含一个元素的数组。
如果没有移除任何元素,则返回一个空数组。
描述
splice() 方法是变异方法。它可能会更改 this 的内容。如果指定的插入元素数量与移除的元素数量不同,数组的 length 也会被更改。同时,它使用 [Symbol.species] 来创建要返回的新数组实例。
如果删除的部分是稀疏的,则 splice() 返回的数组也是稀疏的,具有相应的空槽索引。
splice() 方法是通用的。它只期望 this 值具有 length 属性和整数键属性。尽管字符串也具有类数组的特性,但该方法不适合应用于字符串,因为字符串是不可变的。
示例
在索引 2 之前移除 0 (零) 个元素,并插入 "drum"
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"
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() 一样,将元素插入数组的开头。
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() 一样,将元素插入数组的末尾。
const myFish = ["angel", "clown", "mandarin"];
const removed = myFish.splice(myFish.length, 0, "sturgeon");
// myFish is ["angel", "clown", "mandarin", "sturgeon"]
// no items removed
在索引 3 处移除 1 个元素
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"
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"
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 个元素
const myFish = ["parrot", "anemone", "blue", "trumpet", "sturgeon"];
const removed = myFish.splice(2, 2);
// myFish is ["parrot", "anemone", "sturgeon"]
// removed is ["blue", "trumpet"]
从索引 -2 开始移除 1 个元素
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(-2, 1);
// myFish is ["angel", "clown", "sturgeon"]
// removed is ["mandarin"]
从索引 2 开始移除所有元素
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2);
// myFish is ["angel", "clown"]
// removed is ["mandarin", "sturgeon"]
在稀疏数组上使用 splice()
splice() 方法保留数组的稀疏性。
const arr = [1, , 3, 4, , 6];
console.log(arr.splice(1, 2)); // [empty, 3]
console.log(arr); // [1, 4, empty, 6]
在非数组对象上调用 splice()
splice() 方法读取 this 的 length 属性。然后根据需要更新整数键属性和 length 属性。
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 |
浏览器兼容性
加载中…