// フィルタリングする配列
const num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// 開発者が直接作成したコールバック関数
function isEven(value) {
    return value % 2 === 0; // 偶数の場合はtrueを返す
}

// 配列の要素を走査し、フィルタリングされた配列として返す
const result = num.filter(isEven);
console.log(result); // 出力:[2, 4, 6, 8, 10]
/* filter()関数は配列の中から指定した条件の要素だけをフィルタリングする目的で使用されます。 */

// 次の例は、配列の要素の中から小文字だけをフィルタリングすることが目的です。
[a, B, c, D].filter(小文字) 👉 [a, c]
arr.filter(callbackFn[, thisArg])
// アロー関数
filter((element) => { /* … */ })
filter((element[, index]) => { /* … */ })
filter((element[, index[, array]]) => { /* … */ })

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

// インラインコールバック関数
filter(function (element) { /* … */ })
filter(function (element[, index]) { /* … */ })
filter(function (element[, index[, array]]) { /* … */ })
filter(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]]) { // 名前付き関数の定義
	// フィルタリングするロジック:フィルタリングされた結果を返す必要があります。
}

arr.filter(callbackFn); // 定義した名前付き関数名を引数として直接渡す

/* コールバック関数を匿名関数で使用する場合 */
arr.filter(function (element[, index[, array]]) {
    // フィルタリングするロジック:フィルタリングされた結果を返す必要があります。
});
const numbers = [1, 2, 2, 3, 4, 4, 5];

const uniqueNumbers = numbers.filter((element, index, arr) => {
    // 現在の要素が以前に出現していない場合のみtrueを返す
    return arr.indexOf(element) === index;
});

console.log(uniqueNumbers); // 出力:[1, 2, 3, 4, 5]
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

const filteredNumbers = numbers.filter(element => {
    // 複数条件を組み合わせてフィルタリング
    return element % 2 === 0 && element < 5;
});

console.log(filteredNumbers); // 出力:[2, 4]
// 2次元配列の作成
const students = [
    ["Alice", 25],
    ["Bob", 30],
    ["Charlie", 22],
    ["David", 35]
];

// 年齢が30以上の学生だけをフィルタリング
const filteredStudents = students.filter(student => {
    // student配列の2番目の要素(年齢)が30以上の場合のみtrueを返す
    return student[1] >= 30;
});

console.log(filteredStudents); // 出力:[["Bob", 30], ["David", 35]]
const words = ["apple", "banana", "cherry", "date", "fig"];
const longWords = words.filter((word) => word.length >= 5);
console.log(longWords); // 出力:[ "apple", "banana", "cherry" ]