// 필터링할 배열
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]
[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 배열의 두 번째 요소(나이)가 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"]

caniuse.com에서 더 자세한 정보를 확인해 보세요.