このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docsコミュニティーについてもっと知り、仲間になるにはこちらから。
TypedArray.prototype.findIndex()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2016年9月.
findIndex() はTypedArray インスタンスのメソッドで、指定されたテスト関数を満たす型付き配列の最初の要素のインデックスを返します。テスト関数を満たす要素がない場合、 -1 を返します。このメソッドのアルゴリズムはArray.prototype.findIndex() と同じです。
In this article
試してみましょう
function isNegative(element, index, array) { return element < 0;}const int8 = new Int8Array([10, -20, 30, -40, 50]);console.log(int8.findIndex(isNegative));// Expected output: 1構文
js
findIndex(callbackFn)findIndex(callbackFn, thisArg)引数
callbackFn配列のそれぞれの要素に対して実行する関数です。要素がテストに合格した場合は真値を返し、そうでなければ偽値を返す必要があります。この関数は以下の引数で呼び出されます。
thisArg省略可callbackFnを実行する際にthisとして使用する値。反復処理メソッドを参照してください。
返値
テストを通った配列の要素の位置を返します。それ以外の場合は、-1 を返します。
解説
詳細については、Array.prototype.findIndex() をご覧ください。このメソッドは汎用的ではなく、型付き配列インスタンスに対してのみ呼び出すことができます。
例
>型付き配列内の素数の位置を検索する
次の例では、型付き配列の中で素数の入った最初の要素の位置を返し、素数が見つからなかった場合は-1 を返します。
js
function isPrime(element, index, array) { let start = 2; while (start <= Math.sqrt(element)) { if (element % start++ < 1) { return false; } } return element > 1;}const uint8 = new Uint8Array([4, 6, 8, 12]);const uint16 = new Uint16Array([4, 6, 7, 12]);console.log(uint8.findIndex(isPrime)); // -1, not foundconsole.log(uint16.findIndex(isPrime)); // 2仕様書
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-%typedarray%.prototype.findindex> |