TypedArray.prototype.lastIndexOf()

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

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

Try it

语法

¥Syntax

js
lastIndexOf(searchElement)
lastIndexOf(searchElement, fromIndex)

参数

¥Parameters

searchElement

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

fromIndex Optional

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

返回值

¥Return value

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

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

描述

¥Description

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

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

示例

¥Examples

使用 lastIndexOf()

¥Using lastIndexOf()

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

规范

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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看