find【値検索 (ユーザ関数)】
findIndex【インデックス検索 (ユーザ関数)】
includes【存在確認】
indexOf【検索 (順方向)】
lastIndexOf【検索 (逆方向)】
Array.prototype.find【値検索 (ユーザ関数)】
メモ
- 指定したユーザ関数 (コールバック関数)で順に検索処理を実行し、最初に検索された要素の値を返却
- 関連
- 抽出生成 (複数要素の検索):filter【抽出生成】
- その他検索
- 外部リンク (英語)
Array.prototype.find ( predicate [ , thisArg ] ) ES2022 (13) ES2021 (12) ES2020 (11)
構文
array.find( predicate[, thisArg] )
最初に検索された要素の値 (undefined:検索要素なし)
predicateユーザ関数 (コールバック関数:詳細は下記参照)
thisArgユーザ関数 (コールバック関数)内でthis で参照されるオブジェクト
例外TypeError 例外
predicate が呼び出し可能な関数オブジェクト以外
ユーザ関数 引数名 (例) | 説明 |
---|---|
value | 要素値 |
index | 要素インデックス |
array | Array【配列】オブジェクト |
戻り値型 | 説明 |
Boolean | true:検索対象要素 false:検索対象要素以外 |
例
/**
* find 用ユーザ関数 (コールバック関数) (判定値 this 以上を検索)
* @param {number} value 要素値
* @param {number} index 要素インデックス
* @param {Array} array Array【配列】オブジェクト
* @return {boolean} true:検索対象要素 / false:検索対象要素以外
*/
function findFunc(value, index, array) {
return (this <= value);
}
var array = [ 1, -2, 3, -4, 5 ];
console.log(array.find(findFunc, 0)); // 0 以上を検索
// 出力:1
console.log(array.find(findFunc, 3)); // 3 以上を検索
// 出力:3
console.log(array.find(findFunc, 10)); // 10 以上を検索
// 出力:undefined
Array.prototype.findIndex【インデックス検索 (ユーザ関数)】
メモ
- 指定したユーザ関数 (コールバック関数)で順に検索処理を実行し、最初に検索された要素のインデックスを返却
- 関連
- 抽出生成 (複数要素の検索):filter【抽出生成】
- その他検索
- 外部リンク (英語)
Array.prototype.findIndex ( predicate [ , thisArg ] ) ES2022 (13) ES2021 (12) ES2020 (11)
構文
array.findIndex( predicate[, thisArg] )
要素のインデックス (-1:検索要素なし)
predicateユーザ関数 (コールバック関数:詳細は下記参照)
thisArgユーザ関数 (コールバック関数)内でthis で参照されるオブジェクト
例外TypeError 例外
predicate が呼び出し可能な関数オブジェクト以外
ユーザ関数 引数名 (例) | 説明 |
---|---|
value | 要素値 |
index | 要素インデックス |
array | Array【配列】オブジェクト |
戻り値型 | 説明 |
Boolean | true:検索対象要素 false:検索対象要素以外 |
例
/**
* find 用ユーザ関数 (コールバック関数) (判定値 this 以上を検索)
* @param {number} value 要素値
* @param {number} index 要素インデックス
* @param {Array} array Array【配列】オブジェクト
* @return {boolean} true:検索対象要素 / false:検索対象要素以外
*/
function findFunc(value, index, array) {
return (this <= value);
}
var array = [ 1, -2, 3, -4, 5 ];
console.log(array.findIndex(findFunc, 0)); // 0 以上を検索
// 出力:0
console.log(array.findIndex(findFunc, 3)); // 3 以上を検索
// 出力:2
console.log(array.findIndex(findFunc, 10)); // 10 以上を検索
// 出力:-1
Array.prototype.includes【存在確認】
メモ
- 指定要素が存在するか確認 (未指定の要素も比較対象)
- indexOf【検索 (順方向)】・lastIndexOf【検索 (逆方向)】 (=== 【同値 演算子】で比較) では検索できない要素の検索が可能
- NaN【非数】
- 未指定の要素 (undefined【未定義】で検索可)
- 比較方法の詳細は存在確認比較
- 外部リンク (英語)
Array.prototype.includes ( searchElement [ , fromIndex ] ) ES2022 (13) ES2021 (12) ES2020 (11)
構文
array.includes( searchElement[, fromIndex] )
確認結果
true:要素は存在
false:要素は存在しない
searchElement検索要素
fromIndex検索開始インデックス (詳細は下記参照)
fromIndex | 説明 |
---|---|
省略 | 0 (先頭) |
配列の要素数 ≦ fromIndex | 検索失敗 (-1 返却) |
0 ≦ fromIndex | 先頭から |
fromIndex < 0 (-1:最終要素) | 最終要素からのオフセット (先頭位置より前の場合、先頭インデックス) |
存在確認比較
型 | 検索要素 | 配列内要素 | 結果 |
---|---|---|---|
任意 | 型が相違 | false (不一致) | |
Number【数値】 | NaN | NaN | true (一致) |
+0 | -0 | ||
-0 | +0 | ||
同じ値 | |||
その他 | false (不一致) | ||
Undefined型 | undefined | undefined | true (一致) |
未指定 | |||
Null型 | null | true (一致) | |
String【文字列】 | 長さもコードも一致 | true (一致) | |
その他 | false (不一致) | ||
Boolean【真偽値】 | true | true | true (一致) |
false | false | ||
その他 | false (不一致) | ||
Symbol【シンボル】 | 同じ値 | true (一致) | |
その他 | false (不一致) | ||
その他 Object | 同じ値 | true (一致) | |
その他 | false (不一致) |
例
var arrayPlus = [ 0, 1, 2, 3, 4, 5 ];
console.log(arrayPlus.includes(2)); // 出力:true
console.log(arrayPlus.includes(2, 3)); // 出力:false
var arrayMinus = [ -6, -5, -4, -3, -2, -1 ];
console.log(arrayMinus.includes(-4)); // 出力:true
console.log(arrayMinus.includes(-4, -3)); // 出力:false
var array = [ 0, NaN, , true ];
console.log(array.includes(NaN)); // 出力:true
console.log(array.includes(undefined)); // 出力:true
console.log(array.indexOf(NaN)); // 出力:-1
console.log(array.indexOf(undefined)); // 出力:-1
Array.prototype.indexOf【検索 (順方向)】
メモ
- 要素を順方向に検索 (未指定の要素は比較対象外)
- === 【同値 演算子】で比較
- 下記要素を検索する場合は、includes【存在確認】を使用
- NaN【非数】
- 未指定の要素
- その他検索
- 外部リンク (英語)
Array.prototype.indexOf ( searchElement [ , fromIndex ] ) ES2022 (13) ES2021 (12) ES2020 (11)
構文
array.indexOf( searchElement[, fromIndex] )
一致要素インデックス (-1:一致なし)
searchElement検索要素
fromIndex検索開始インデックス (詳細は下記参照)
fromIndex | 説明 |
---|---|
省略 | 0 (先頭) |
配列の要素数 ≦ fromIndex | 検索失敗 (-1 返却) |
0 ≦ fromIndex | 先頭から |
fromIndex < 0 (-1:最終要素) | 最終要素からのオフセット (先頭位置より前の場合、先頭インデックス) |
例
var array = ["one", "same", "same", null, NaN, "six"];
// [0] [1] [2] [3] [4] [5]
// [-6] [-5] [-4] [-3] [-2] [-1]
// 一致あり
console.log(array.indexOf("same")); // 出力:1
console.log(array.indexOf("same", 1)); // 出力:1
console.log(array.indexOf("same", -5)); // 出力:1
console.log(array.indexOf("same", -99));// 出力:1
console.log(array.indexOf("same", 2)); // 出力:2
console.log(array.indexOf(null)); // 出力:3
// 一致なし
console.log(array.indexOf("same", 3)); // 出力:-1
console.log(array.indexOf("same", -3)); // 出力:-1
console.log(array.indexOf("sa")); // 出力:-1
console.log(array.indexOf("same", 99)); // 出力:-1
console.log(array.indexOf(NaN)); // 出力:-1 (NaN === NaN) が false の為
Array.prototype.lastIndexOf【検索 (逆方向)】
メモ
- 要素を逆方向に検索 (未指定の要素は比較対象外)
- === 【同値 演算子】で比較
- 下記要素を検索する場合は、includes【存在確認】を使用
- NaN【非数】
- 未指定の要素
- その他検索
- 外部リンク (英語)
Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) ES2022 (13) ES2021 (12) ES2020 (11)
構文
array.lastIndexOf( searchElement[, fromIndex] )
一致要素インデックス (-1:一致なし)
searchElement検索要素
fromIndex検索開始インデックス (詳細は下記参照)
fromIndex | 説明 |
---|---|
省略 | 0 (先頭) |
配列の要素数 ≦ fromIndex | 検索失敗 (-1 返却) |
0 ≦ fromIndex | 先頭から |
fromIndex < 0 (-1:最終要素) | 最終要素からのオフセット (先頭位置より前の場合、先頭インデックス) |
例
var array = ["one", "same", "same", null, NaN, "six"];
// [0] [1] [2] [3] [4] [5]
// [-6] [-5] [-4] [-3] [-2] [-1]
// 一致あり
console.log(array.lastIndexOf("same")); // 出力:2
console.log(array.lastIndexOf("same", 2)); // 出力:2
console.log(array.lastIndexOf("same", -4)); // 出力:2
console.log(array.lastIndexOf("same", 1)); // 出力:1
console.log(array.lastIndexOf("same", -5)); // 出力:1
console.log(array.lastIndexOf(null)); // 出力:3
// 一致なし
console.log(array.lastIndexOf("same", 0)); // 出力:-1
console.log(array.lastIndexOf("same", -6)); // 出力:-1
console.log(array.lastIndexOf("sa")); // 出力:-1
console.log(array.lastIndexOf("same", -99)); // 出力:-1
console.log(array.lastIndexOf(NaN)); // 出力:-1 (NaN === NaN) が false の為