find【値検索 (ユーザ関数)】
findIndex【インデックス検索 (ユーザ関数)】
includes【存在確認】
indexOf【検索 (順方向)】
lastIndexOf【検索 (逆方向)】

Array.prototype.find【値検索 (ユーザ関数)】

メモ

構文

array.find( predicate[, thisArg] )

最初に検索された要素の値 (undefined:検索要素なし)
predicateユーザ関数 (コールバック関数:詳細は下記参照)
thisArgユーザ関数 (コールバック関数)内でthis で参照されるオブジェクト
例外TypeError 例外
predicate が呼び出し可能な関数オブジェクト以外
ユーザ関数 引数名 (例)説明
value要素値
index要素インデックス
arrayArray【配列】オブジェクト
戻り値型説明
Booleantrue:検索対象要素
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【インデックス検索 (ユーザ関数)】

メモ

構文

array.findIndex( predicate[, thisArg] )

要素のインデックス (-1:検索要素なし)
predicateユーザ関数 (コールバック関数:詳細は下記参照)
thisArgユーザ関数 (コールバック関数)内でthis で参照されるオブジェクト
例外TypeError 例外
predicate が呼び出し可能な関数オブジェクト以外
ユーザ関数 引数名 (例)説明
value要素値
index要素インデックス
arrayArray【配列】オブジェクト
戻り値型説明
Booleantrue:検索対象要素
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【存在確認】

メモ

構文

array.includes( searchElement[, fromIndex] )

確認結果
true:要素は存在
false:要素は存在しない
searchElement検索要素
fromIndex検索開始インデックス (詳細は下記参照)
fromIndex説明
省略0 (先頭)
配列の要素数 ≦ fromIndex検索失敗 (-1 返却)
0 ≦ fromIndex先頭から
fromIndex < 0
(-1:最終要素)
最終要素からのオフセット (先頭位置より前の場合、先頭インデックス)

存在確認比較

検索要素配列内要素結果
任意型が相違false (不一致)
Number【数値】NaNNaNtrue (一致)
+0-0
-0+0
同じ値
その他false (不一致)
Undefined型undefinedundefinedtrue (一致)
未指定
Null型nulltrue (一致)
String【文字列】長さもコードも一致true (一致)
その他false (不一致)
Boolean【真偽値】truetruetrue (一致)
falsefalse
その他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【検索 (順方向)】

メモ

構文

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【検索 (逆方向)】

メモ

構文

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 の為