TypedArray.prototype.includes()

Baseline Widely available

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

TypedArray 实例的 includes() 方法确定类型化数组的条目中是否包含某个值,并根据需要返回 truefalse。该方法与 Array.prototype.includes() 的算法相同。

¥The includes() method of TypedArray instances determines whether a typed array includes a certain value among its entries, returning true or false as appropriate. This method has the same algorithm as Array.prototype.includes().

Try it

语法

¥Syntax

js
includes(searchElement)
includes(searchElement, fromIndex)

参数

¥Parameters

searchElement

要搜索的值。

fromIndex Optional

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

返回值

¥Return value

如果在类型化数组(或由索引 fromIndex 指示的类型化数组的一部分,如果指定)中找到值 searchElement,则为 true 的布尔值。

¥A boolean value which is true if the value searchElement is found within the typed array (or the part of the typed array indicated by the index fromIndex, if specified).

描述

¥Description

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

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

示例

¥Examples

使用包含()

¥Using includes()

js
const uint8 = new Uint8Array([1, 2, 3]);
uint8.includes(2); // true
uint8.includes(4); // false
uint8.includes(3, 3); // false

// NaN handling (only relevant for floating point arrays)
new Uint8Array([NaN]).includes(NaN); // false, since the NaN passed to the constructor gets converted to 0
new Float32Array([NaN]).includes(NaN); // true

规范

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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看