[😃, 🏀, 🍎, 🍇, 🐵].findIndex(function(検索要素) {
    return 検索要素 === 果物;
}); // 👉 2

// この配列では、検索条件である果物(🍎、🍇)のうち
// 最初の果物(🍎)のインデックス(2)が返されます。
// 配列を作成します。
const numbers = [1, 3, 7, 4, 6, 8];

// 条件(偶数)を満たす最初の要素のインデックスを取得します。
const firstEvenIndex = numbers.findIndex(function(num) {
    return num % 2 === 0; // 偶数かどうかを確認
});

// 結果を出力します。
// 条件(偶数)を満たす最初の要素は4で、
// その要素のインデックスは3です。
console.log(firstEvenIndex); // 出力: 3
arr.findIndex(callbackFn[, thisArg])
// アロー関数
findIndex((element) => { /* … */ })
findIndex((element[, index]) => { /* … */ })
findIndex((element[, index[, array]]) => { /* … */ })

// コールバック関数
findIndex(callbackFn)
findIndex(callbackFn[, thisArg])

// インラインコールバック関数
findIndex(function (element) { /* … */ })
findIndex(function (element[, index]) { /* … */ })
findIndex(function (element[, index[, array]]) { /* … */ })
findIndex(function (element[, index[, array]]) { /* … */ }[, thisArg])
callbackFn(element[, index[, array]])
/**
 * コールバック関数
 *
 * @param {*} element 配列の各要素
 * @param {number} index 配列のインデックス(任意)
 * @param {Array} array 元の配列(任意)
 * @return {boolean} フィルタリング条件を満たす場合はtrue、満たさない場合はfalseを返します
 *
 * コールバック関数は、名前付き関数(ユーザー定義関数)や匿名関数などで使用できます。
 * (もちろん)すべてのコールバック関数はアロー関数としても使用可能です。
 */

/* コールバック関数を名前付き関数で使用する場合 */
function callbackFn(element[, index[, array]]) { // 名前付き関数の定義
    // return文を使用して条件を定義します。
}

arr.findIndex(callbackFn); // 定義した名前付き関数をパラメーターとして直接渡します

/* コールバック関数を匿名関数で使用する場合 */
arr.findIndex(function (element[, index[, array]]) {
    // return文を使用して条件を定義します。
});
// 配列に特定の要素が存在するかの確認
// 配列内で条件を満たす要素のインデックスを使って
// 存在の有無を判定できます。

const numbers = [1, 2, 3, 4, 5];
const target = 3;

// 条件を満たす最初の要素のインデックスを取得します。
const index = numbers.findIndex(element => element === target);

// 結果に応じてメッセージを出力します。
if (index !== -1) {
    console.log(`検索する値(${target})は配列に存在します。インデックス: ${index}`);
} else {
    console.log(`検索する値(${target})は配列に存在しません。`);
}

// 出力: 検索する値(3)は配列に存在します。インデックス: 2
// ユーザーログイン記録の配列
const loginHistory = [
  { user: 'Alice', date: '2025-01-01' },
  { user: 'Bob', date: '2025-01-02' },
  { user: 'Alice', date: '2025-01-05' },
  { user: 'Charlie', date: '2025-01-06' },
  { user: 'Alice', date: '2025-01-10' },
  { user: 'Bob', date: '2025-01-12' },
];

// 特定のユーザー
const targetUser = 'Alice';

// 最初のログイン(findIndex)
const firstLoginIndex = loginHistory.findIndex(record => record.user === targetUser);

if (firstLoginIndex !== -1) {
  const firstLogin = loginHistory[firstLoginIndex];
  console.log(`最初のログイン:
    - ユーザー: ${firstLogin.user}
    - 日付: ${firstLogin.date}`);
} else {
  console.log(`${targetUser}の最初のログイン記録はありません。`);
}

/* 出力:
最初のログイン:
    - ユーザー: Alice
    - 日付: 2025-01-01
*/