TypedArray.prototype.indexOf()

TypedArray 实例的 indexOf() 方法返回在类型化数组中可以找到给定元素的第一个索引,如果不存在则返回 -1。该方法与 Array.prototype.indexOf() 的算法相同。

¥The indexOf() method of TypedArray instances returns the first index at which a given element can be found in the typed array, or -1 if it is not present. This method has the same algorithm as Array.prototype.indexOf().

Try it

语法

¥Syntax

js
indexOf(searchElement)
indexOf(searchElement, fromIndex)

参数

¥Parameters

searchElement

要在类型化数组中定位的元素。

fromIndex Optional

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

返回值

¥Return value

类型化数组中 searchElement 的第一个索引;如果未找到,则为 -1

¥The first index of searchElement in the typed array; -1 if not found.

描述

¥Description

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

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

示例

¥Examples

使用 indexOf()

¥Using indexOf()

js
const uint8 = new Uint8Array([2, 5, 9]);
uint8.indexOf(2); // 0
uint8.indexOf(7); // -1
uint8.indexOf(9, 2); // 2
uint8.indexOf(2, -1); // -1
uint8.indexOf(2, -3); // 0

规范

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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看