TypedArray.prototype.find()
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月.
find() は TypedArray インスタンスのメソッドで、型付き配列のある要素の値が与えられたテスト関数を満たした場合、その値を返します。そうでなければ undefined を返します。このメソッドのアルゴリズムは Array.prototype.find() と同じです。
試してみましょう
function isNegative(element, index, array) {
return element < 0;
}
const int8 = new Int8Array([10, 0, -10, 20, -30, 40, -50]);
console.log(int8.find(isNegative));
// Expected output: -10
構文
js
find(callbackFn)
find(callbackFn, thisArg)
引数
callbackFn-
配列のそれぞれの要素に対して実行する関数です。要素がテストに合格した場合は真値を返し、そうでなければ偽値を返す必要があります。この関数は以下の引数で呼び出されます。
thisArg省略可-
callbackFnを実行する際にthisとして使用する値。反復処理メソッドを参照してください。
返値
テストを満たした配列の要素の位置を返します。それ以外の場合は、 undefined を返します。
解説
詳細については、 Array.prototype.find() をご覧ください。このメソッドは汎用的ではなく、型付き配列インスタンスに対してのみ呼び出すことができます。
例
>型付き配列内の素数を探す
次の例では、型付き配列内で素数である要素を探します(または、素数がない場合は undefined を返します)。
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, 5, 8, 12]);
console.log(uint8.find(isPrime)); // 5
仕様書
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-%typedarray%.prototype.find> |
ブラウザーの互換性
Loading…
関連情報
TypedArray.prototype.findのポリフィル (core-js)- JavaScript の型付き配列ガイド
TypedArrayTypedArray.prototype.findIndex()TypedArray.prototype.findLast()TypedArray.prototype.findLastIndex()TypedArray.prototype.includes()TypedArray.prototype.filter()TypedArray.prototype.every()TypedArray.prototype.some()Array.prototype.find()