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()
方法将数组的一部分的 浅拷贝 返回到从 start
到 end
(不包括 end
)中选择的新数组对象中,其中 start
和 end
表示该数组中项目的索引。原始数组不会被修改。
¥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
语法
参数
¥Parameters
start
Optional-
开始提取的从零开始的索引,转换为整数。
- 负索引从数组末尾开始倒数 — 如果使用
-array.length <= start < 0
、start + array.length
。 - 如果省略
start < -array.length
或start
,则使用0
。 - 如果是
start >= array.length
,则返回一个空数组。
- 负索引从数组末尾开始倒数 — 如果使用
end
Optional-
结束提取的从零开始的索引,转换为整数。
slice()
提取直至但不包括end
。- 负索引从数组末尾开始倒数 — 如果使用
-array.length <= end < 0
、end + array.length
。 - 如果使用
end < -array.length
、0
。 - 如果省略
end >= array.length
或end
,则使用array.length
,导致提取直到末尾的所有元素。 - 如果
end
暗示start
暗示的位置之前或位置,则返回一个空数组。
- 负索引从数组末尾开始倒数 — 如果使用
返回值
描述
¥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.
示例
返回现有数组的一部分
使用切片
¥Using slice
在以下示例中,slice
从 myCar
创建一个新数组 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.
// 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()
方法读取 this
的 length
属性。然后,它读取从 start
到 end
的整数键属性,并在新创建的数组上定义它们。
¥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.
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.
// 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()
规范
Specification |
---|
ECMAScript Language Specification # sec-array.prototype.slice |
浏览器兼容性
BCD tables only load in the browser