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 |
瀏覽器相容性
載入中…