Array.prototype.slice()

Baseline 已广泛支持

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

slice() 方法 Array 实例会返回一个 浅拷贝,该拷贝包含从 startend(不包含 end)的部分数组,其中 startend 表示数组中元素的索引。原始数组不会被修改。

试一试

const animals = ["ant", "bison", "camel", "duck", "elephant"];

console.log(animals.slice(2));
// Expected output: Array ["camel", "duck", "elephant"]

console.log(animals.slice(2, 4));
// Expected output: Array ["camel", "duck"]

console.log(animals.slice(1, 5));
// Expected output: Array ["bison", "camel", "duck", "elephant"]

console.log(animals.slice(-2));
// Expected output: Array ["duck", "elephant"]

console.log(animals.slice(2, -1));
// Expected output: Array ["camel", "duck"]

console.log(animals.slice());
// Expected output: Array ["ant", "bison", "camel", "duck", "elephant"]

语法

js
slice()
slice(start)
slice(start, end)

参数

start 可选

用于开始提取的零基索引,转换为整数

  • 负数索引从数组末尾开始计数——如果 -array.length <= start < 0,则使用 start + array.length
  • 如果 start < -array.length 或省略了 start,则使用 0
  • 如果 start >= array.length,则返回一个空数组。
end 可选

用于结束提取的零基索引,转换为整数slice() 会提取到 end 之前(不包含 end)。

  • 负数索引从数组末尾开始计数——如果 -array.length <= end < 0,则使用 end + array.length
  • 如果 end < -array.length,则使用 0
  • 如果 end >= array.length 或省略了 endendundefined,则使用 array.length,这将提取到数组末尾的所有元素。
  • 如果 end 表示的位置在 start 表示的位置之前或与之相同,则返回一个空数组。

返回值

一个包含提取元素的新数组。

描述

slice() 方法是一个复制方法。它不会修改 this,而是返回一个浅拷贝,其中包含原始数组中的部分元素。

slice() 方法会保留空槽。如果切片部分是稀疏的,则返回的数组也会是稀疏的。

slice() 方法是通用的。它只要求 this 值具有 length 属性和整数键属性。

示例

返回现有数组的一部分

js
const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const citrus = fruits.slice(1, 3);

// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']

在此示例中,slice(1, 3) 从索引 1 提取元素直到索引 3(不包含),生成新数组 ['Orange', 'Lemon']

省略 end 参数

js
const fruits = ["Apple", "Banana", "Orange", "Mango", "Pineapple"];

const tropical = fruits.slice(2);
console.log(tropical); // ['Orange', 'Mango', 'Pineapple']

在此示例中,slice(2) 从索引 2 提取到数组末尾的元素。

使用负数索引

js
const fruits = ["Apple", "Banana", "Orange", "Mango", "Pineapple"];

const lastTwo = fruits.slice(-2);
console.log(lastTwo); // ['Mango', 'Pineapple']

在此示例中,slice(-2) 提取数组的最后两个元素。当对 slice 方法使用负数索引时,负数索引从数组末尾开始计数,最后一个元素为 -1,倒数第二个元素为 -2,依此类推。负数索引 -2 本身被包含在内,因为它是提取的起始点。

|     |     |     |     |     |
|  S  |  L  |  I  |  C  |  E  |
|     |     |     |     |     |
  -5    -4    -3    -2    -1

<--- read from reverse

使用正数 start 索引和负数 end 索引

js
const fruits = ["Apple", "Banana", "Orange", "Mango", "Pineapple"];

// Using positive start index and negative end index
const sliceExample = fruits.slice(1, -1);
console.log(sliceExample); // ['Banana', 'Orange', 'Mango']

在此示例中,slice(1, -1) 从索引 1 开始提取,直到索引 -1(即最后一个元素)之前(不包含)。这将生成新数组 ['Banana', 'Orange', 'Mango']slice 方法始终排除最后一个指定索引处的元素,无论该索引是正数还是负数。

read from start --->

   0     1     2     3     4
|     |     |     |     |     |
|  S  |  L  |  I  |  C  |  E  |
|     |     |     |     |     |
  -5    -4    -3    -2    -1

<--- read from reverse

对对象数组使用 slice

在下面的示例中,slicemyCar 创建了一个新数组 newCar。两者都包含对对象 myHonda 的引用。当 myHonda 的颜色更改为紫色时,两个数组都会反映出此更改。

js
// Using slice, create newCar from myCar.
const myHonda = {
  color: "red",
  wheels: 4,
  engine: { cylinders: 4, size: 2.2 },
};
const myCar = [myHonda, 2, "cherry condition", "purchased 1997"];
const newCar = myCar.slice(0, 2);

console.log("myCar =", myCar);
console.log("newCar =", newCar);
console.log("myCar[0].color =", myCar[0].color);
console.log("newCar[0].color =", newCar[0].color);

// Change the color of myHonda.
myHonda.color = "purple";
console.log("The new color of my Honda is", myHonda.color);

console.log("myCar[0].color =", myCar[0].color);
console.log("newCar[0].color =", newCar[0].color);

此脚本输出

myCar = [
  { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } },
  2,
  'cherry condition',
  'purchased 1997'
]
newCar = [ { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } }, 2 ]
myCar[0].color = red
newCar[0].color = red
The new color of my Honda is purple
myCar[0].color = purple
newCar[0].color = purple

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

slice() 方法会读取 thislength 属性。然后,它会读取从 startend 的整数键属性,并在新创建的数组上定义它们。

js
const arrayLike = {
  length: 3,
  0: 2,
  1: 3,
  2: 4,
  3: 33, // ignored by slice() since length is 3
};
console.log(Array.prototype.slice.call(arrayLike, 1, 3));
// [ 3, 4 ]

使用 slice() 将类数组对象转换为数组

slice() 方法通常与 bind()call() 一起使用,创建一个将类数组对象转换为数组的实用方法。

js
// slice() is called with `this` passed as the first argument
const slice = Function.prototype.call.bind(Array.prototype.slice);

function list() {
  return slice(arguments);
}

const listResult = list(1, 2, 3); // [1, 2, 3]

在稀疏数组上使用 slice()

如果源数组是稀疏的,则从 slice() 返回的数组也可能是稀疏的。

js
console.log([1, 2, , 4, 5].slice(1, 4)); // [2, empty, 4]

规范

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

浏览器兼容性

另见