TypedArray.prototype.lastIndexOf()

Baseline Widely available

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

lastIndexOf()TypedArray インスタンスのメソッドで、指定された要素が型付き配列内で見つかった最後の位置のインデックスを返し、存在しなければ -1 を返します。型付き配列は fromIndex で始まる位置から逆方向に検索されます。 このメソッドは Array.prototype.lastIndexOf() と同じアルゴリズムです。

試してみましょう

構文

js
lastIndexOf(searchElement)
lastIndexOf(searchElement, fromIndex)

引数

searchElement

型付き配列内で検索する要素。

fromIndex 省略可

検索を始める位置を示すゼロ基点のインデックスで、整数に変換されます

返値

型付き配列内における最後の searchElement のインデックスです。見つからなかったら -1 になります。

解説

詳細については、 Array.prototype.lastIndexOf() をご覧ください。このメソッドは汎用的ではなく、型付き配列インスタンスに対してのみ呼び出すことができます。

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

ブラウザーの互換性

BCD tables only load in the browser

関連情報