Array.prototype.slice()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Array 实例的 slice() 方法将数组的一部分的 浅拷贝 返回到从 startend(不包括 end)中选择的新数组对象中,其中 startend 表示该数组中项目的索引。原始数组不会被修改。

¥The slice() method of Array instances returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.

Try it

语法

¥Syntax

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

参数

¥Parameters

start Optional

开始提取的从零开始的索引,转换为整数

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

结束提取的从零开始的索引,转换为整数slice() 提取直至但不包括 end

  • 负索引从数组末尾开始倒数 — 如果使用 -array.length <= end < 0end + array.length
  • 如果使用 end < -array.length0
  • 如果省略 end >= array.lengthend,则使用 array.length,导致提取直到末尾的所有元素。
  • 如果 end 暗示 start 暗示的位置之前或位置,则返回一个空数组。

返回值

¥Return value

包含提取元素的新数组。

¥A new array containing the extracted elements.

描述

¥Description

slice() 方法是 复印法 方法。它不会更改 this,而是返回 浅拷贝,其中包含一些与原始数组中的元素相同的元素。

¥The slice() method is a copying method. It does not alter this but instead returns a shallow copy that contains some of the same elements as the ones from the original array.

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

¥The slice() method preserves empty slots. If the sliced portion is sparse, the returned array is sparse as well.

slice() 方法是 generic。它只期望 this 值具有 length 属性和整数键控属性。

¥The slice() method is generic. It only expects the this value to have a length property and integer-keyed properties.

示例

¥Examples

返回现有数组的一部分

¥Return a portion of an existing array

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']

使用切片

¥Using slice

在以下示例中,slicemyCar 创建一个新数组 newCar。两者都包含对对象 myHonda 的引用。当 myHonda 的颜色变为紫色时,两个数组都会反映该变化。

¥In the following example, slice creates a new array, newCar, from myCar. Both include a reference to the object myHonda. When the color of myHonda is changed to purple, both arrays reflect the change.

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);

这个脚本写道:

¥This script writes:

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()

¥Calling slice() on non-array objects

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

¥The slice() method reads the length property of this. It then reads the integer-keyed properties from start to end and defines them on a newly created array.

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() 将类似数组的对象转换为数组

¥Using slice() to convert array-like objects to arrays

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

¥The slice() method is often used with bind() and call() to create a utility method that converts an array-like object into an array.

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 list1 = list(1, 2, 3); // [1, 2, 3]

在稀疏数组上使用 slice()

¥Using slice() on sparse arrays

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

¥The array returned from slice() may be sparse if the source is sparse.

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

规范

Specification
ECMAScript Language Specification
# sec-array.prototype.slice

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看