// 배열을 만듭니다.
const colors = ["red", "green", "blue"];

// 개발자가 직접 작성한 콜백 함수
function findGreen(color) {
    return color === "green"; // 배열 요소가 "green"과 같은지 확인합니다.
};

// 배열에서 조건을 만족하는 첫 번째 요소를 찾아 인덱스를 반환합니다.
const green = colors.findIndex(findGreen);

// 결과를 출력합니다.
console.log(green); // 출력: 1
arr.findIndex(callbackFn[, 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;

// findIndex() 함수를 사용하여 특정 값의 위치를 찾기
const index = numbers.findIndex(num => num === target);

if (index !== -1) {
  console.log(`값 ${target}의 위치는 ${index + 1} 번째입니다.`);
} else {
  console.log(`값 ${target}을 찾을 수 없습니다.`);
}

// 출력: '값 3의 위치는 3 번째입니다.'
// 문서 목록
const documents = [
    "JavaScript는 웹 프로그래밍 언어입니다.",
    "Node.js를 사용하여 서버를 구축할 수 있습니다.",
    "React는 인기 있는 프론트엔드 라이브러리입니다.",
    "Python은 범용 프로그래밍 언어입니다."
];
  
// 찾고자 하는 키워드
const targetKeyword = "Node.js";

// findIndex() 함수를 사용하여 첫 번째로 특정 키워드를 포함하는 문서를 찾습니다.
const index = documents.findIndex(document => document.includes(targetKeyword));

if (index !== -1) {
    console.log(`"${targetKeyword}"를 포함하는 첫 번째 문서의 위치: ${index + 1}`);
} else {
    console.log(`"${targetKeyword}"를 포함하는 문서를 찾을 수 없습니다.`);
}

// 출력 '"Node.js"를 포함하는 첫 번째 문서의 위치: 2'
// 실시간 알림 이벤트 목록
const notificationEvents = [
    {id: 1, message: "새 메시지 도착", recipient: "Alice"},
    {id: 2, message: "주문 완료", recipient: "Bob"},
    {id: 3, message: "미팅 시작", recipient: "Charlie"},
    {id: 4, message: "할일 알림", recipient: "Alice"}
];

// 특정 사용자에게 전달해야 하는 알림 대상
const targetRecipient = "Alice";

// findIndex() 함수를 사용하여 첫 번째로 특정 사용자에게 전달해야 하는 알림을 찾습니다.
const index = notificationEvents.findIndex(event => event.recipient === targetRecipient);

if (index !== -1) {
    const firstNotification = notificationEvents[index];
    console.log(`"${targetRecipient}"에게 전달해야 하는 첫 번째 알림: ${firstNotification.message}`);
    // 여기에서 알림을 사용자에게 전달하는 로직을 추가할 수 있습니다.
} else {
    console.log(`"${targetRecipient}"에게 전달해야 하는 알림을 찾을 수 없습니다.`);
}

// 출력: '"Alice"에게 전달해야 하는 첫 번째 알림: 새 메시지 도착'
// 실시간 알림 이벤트 목록
const notificationEvents = [
    {id: 1, message: "새 메시지 도착", recipient: "Alice"},
    {id: 2, message: "주문 완료", recipient: "Bob"},
    {id: 3, message: "미팅 시작", recipient: "Charlie"},
    {id: 4, message: "할일 알림", recipient: "Alice"}
];

// 특정 사용자에게 전달해야 하는 알림 대상
const targetRecipient = "Alice";

// find() 함수를 사용하여 첫 번째로 특정 사용자에게 전달해야 하는 알림을 찾습니다.
const firstNotification = notificationEvents.find(event => event.recipient === targetRecipient);

if (firstNotification) {
    console.log(`"${targetRecipient}"에게 전달해야 하는 첫 번째 알림: ${firstNotification.message}`);
    // 여기에서 알림을 사용자에게 전달하는 로직을 추가할 수 있습니다.
} else {
    console.log(`"${targetRecipient}"에게 전달해야 하는 알림을 찾을 수 없습니다.`);
}

// 출력: '"Alice"에게 전달해야 하는 첫 번째 알림: 새 메시지 도착'
findIndex() 함수에 대한 브라우저 호환성
메서드
데스크탑 Chrome
Chrome
데스크탑데스크탑 Edge
Edge
데스크탑 Firefox
Firefox
Safari
Safari
findIndex() 48 12 25 8

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