Array.prototype.splice()
splice()
方法通过移除或者替换已存在的元素和/或添加新元素就地改变一个数组的内容。
要创建一个删除和/或替换部分内容而不改变原数组的新数组,请使用 toSpliced()
。要访问数组的一部分而不修改它,参见 slice()
。
尝试一下
语法
js
splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2, itemN)
参数
start
-
从 0 开始计算的索引,表示要开始改变数组的位置,它会被转换成整数。
- 负索引从数组末尾开始计算——如果
start < 0
,使用start + array.length
。 - 如果
start < -array.length
,使用0
。 - 如果
start >= array.length
,则不会删除任何元素,但是该方法会表现为添加元素的函数,添加所提供的那些元素。 - 如果
start
被省略了(即调用splice()
时不传递参数),则不会删除任何元素。这与传递undefined
不同,后者会被转换为0
。
- 负索引从数组末尾开始计算——如果
deleteCount
可选-
一个整数,表示数组中要从
start
开始删除的元素数量。如果省略了
deleteCount
,或者其值大于或等于由start
指定的位置到数组末尾的元素数量,那么从start
到数组末尾的所有元素将被删除。但是,如果你想要传递任何itemN
参数,则应向deleteCount
传递Infinity
值,以删除start
之后的所有元素,因为显式的undefined
会转换为0
。如果
deleteCount
是0
或者负数,则不会移除任何元素。在这种情况下,你应该至少指定一个新元素(请参见下文)。 item1
, …,itemN
可选-
从
start
开始要加入到数组中的元素。如果不指定任何元素,
splice()
将只从数组中删除元素。
返回值
一个包含了删除的元素的数组。
如果只移除一个元素,则返回一个元素的数组。
如果没有删除任何元素,则返回一个空数组。
描述
示例
移除索引 2 之前的 0(零)个元素,并插入“drum”
js
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");
// 运算后的 myFish 是 ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed 是 [],没有元素被删除
移除索引 2 之前的 0(零)个元素,并插入“drum”和“guitar”
js
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum", "guitar");
// 运算后的 myFish 是 ["angel", "clown", "drum", "guitar", "mandarin", "sturgeon"]
// removed 是 [],没有元素被删除
在索引 3 处移除 1 个元素
js
const myFish = ["angel", "clown", "drum", "mandarin", "sturgeon"];
const removed = myFish.splice(3, 1);
// 运算后的 myFish 是 ["angel", "clown", "drum", "sturgeon"]
// removed 是 ["mandarin"]
在索引 2 处移除 1 个元素,并插入“trumpet”
js
const myFish = ["angel", "clown", "drum", "sturgeon"];
const removed = myFish.splice(2, 1, "trumpet");
// 运算后的 myFish 是 ["angel", "clown", "trumpet", "sturgeon"]
// removed 是 ["drum"]
从索引 0 处移除 2 个元素,并插入“parrot”、“anemone”和“blue”
js
const myFish = ["angel", "clown", "trumpet", "sturgeon"];
const removed = myFish.splice(0, 2, "parrot", "anemone", "blue");
// 运算后的 myFish 是 ["parrot", "anemone", "blue", "trumpet", "sturgeon"]
// removed 是 ["angel", "clown"]
从索引 2 处开始移除 2 个元素
js
const myFish = ["parrot", "anemone", "blue", "trumpet", "sturgeon"];
const removed = myFish.splice(2, 2);
// 运算后的 myFish 是 ["parrot", "anemone", "sturgeon"]
// removed 是 ["blue", "trumpet"]
从索引 -2 处移除 1 个元素
js
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(-2, 1);
// 运算后的 myFish 是 ["angel", "clown", "sturgeon"]
// removed 是 ["mandarin"]
从索引 2 开始删除所有元素
js
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2);
// 运算后的 myFish 是 ["angel", "clown"]
// removed 是 ["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()
方法读取 this
的 length
属性。然后,它根据需要更新整数键属性和 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' }
规范
Specification |
---|
ECMAScript Language Specification # sec-array.prototype.splice |
浏览器兼容性
BCD tables only load in the browser