TypedArray.prototype.slice()

TypedArray 实例的 slice() 方法将类型化数组的一部分的副本返回到从 startend(不包括 end)中选择的新类型化数组对象中,其中 startend 表示该类型化数组中项目的索引。原始类型化数组不会被修改。该方法与 Array.prototype.slice() 的算法相同。

¥The slice() method of TypedArray instances returns a copy of a portion of a typed array into a new typed array object selected from start to end (end not included) where start and end represent the index of items in that typed array. The original typed array will not be modified. This method has the same algorithm as Array.prototype.slice().

Try it

语法

¥Syntax

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

参数

¥Parameters

start Optional

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

end Optional

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

返回值

¥Return value

包含提取元素的新类型数组。

¥A new typed array containing the extracted elements.

描述

¥Description

详细信息请参见 Array.prototype.slice()。此方法不是通用的,只能在类型化数组实例上调用。

¥See Array.prototype.slice() for more details. This method is not generic and can only be called on typed array instances.

示例

¥Examples

返回现有类型化数组的一部分

¥Return a portion of an existing typed array

js
const uint8 = new Uint8Array([1, 2, 3]);
uint8.slice(1); // Uint8Array [ 2, 3 ]
uint8.slice(2); // Uint8Array [ 3 ]
uint8.slice(-2); // Uint8Array [ 2, 3 ]
uint8.slice(0, 1); // Uint8Array [ 1 ]

规范

Specification
ECMAScript Language Specification
# sec-%typedarray%.prototype.slice

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看