Movatterモバイル変換


[0]ホーム

URL:


  1. 面向开发者的 Web 技术
  2. JavaScript
  3. JavaScript 参考
  4. JavaScript 标准内置对象
  5. Array
  6. Array.prototype.splice()

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in EnglishAlways switch to English

Array.prototype.splice()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

splice() 方法就地移除或者替换已存在的元素和/或添加新的元素。

要创建一个删除和/或替换部分内容而不改变原数组的新数组,请使用toSpliced()。要访问数组的一部分而不修改它,参见slice()

尝试一下

const months = ["Jan", "March", "April", "June"];months.splice(1, 0, "Feb");// Inserts at index 1console.log(months);// Expected output: Array ["Jan", "Feb", "March", "April", "June"]months.splice(4, 1, "May");// Replaces 1 element at index 4console.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

从 0 开始计算的索引,表示要开始改变数组的位置,它会被转换成整数

  • 负索引从数组末尾开始计算——如果-buffer.length <= 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

如果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 是 ["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 是 [],没有移除的元素

在索引 0 处移除 0 个元素,并插入“angel”

splice(0, 0, ...elements)unshift() 一样在数组的开头插入元素。

js
const myFish = ["clown", "mandarin", "sturgeon"];const removed = myFish.splice(0, 0, "angel");// myFish 是 ["angel", "clown", "mandarin", "sturgeon"]// 没有移除的元素

在最后一个索引处移除 0 个元素,并插入“sturgeon”

splice(array.length, 0, ...elements)push() 一样在数组的末尾插入元素。

js
const myFish = ["angel", "clown", "mandarin"];const removed = myFish.splice(myFish.length, 0, "sturgeon");// myFish 是 ["angel", "clown", "mandarin", "sturgeon"]// 没有移除的元素

在索引 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() 方法读取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' }

规范

Specification
ECMAScript® 2026 Language Specification
# sec-array.prototype.splice

浏览器兼容性

参见

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp