ํ๋ณ ํจ์๋ฅผ ๋ง์กฑํ๋ ์ฒซ ์์ ๋ฐํ
arr.findIndex(callback)
1. callback ํจ์
2. findIndex ํจ์
3. callback ํจ์๊ฐ ๋ฐ๋ ์ธ์
callback(element, index, array)
const array1 = [5, 12, 8, 130, 44];
const found = array1.find((element) => element > 10);
console.log(found); // 12
find์ ๋น์ทํ์ง๋ง,
ํ๋ณ ํจ์๋ฅผ ๋ง์กฑํ๋ ์ฒซ ์ธ๋ฑ์ค ๋ฐํ
arr.findIndex(callback)
1. callback ํจ์
2. findIndex ํจ์
3. callback ํจ์๊ฐ ๋ฐ๋ ์ธ์
callback(element, index, array)
const hobbies = ["Sports", "Cooking", "๋ ์ฐพ์", "Reading"];
const index = hobbies.findIndex((item) => item === "๋ ์ฐพ์"); //์์ ์ํ
/* ์๋ ๋์ผ
hobbies.findIndex((item) => {
return item === "๋ ์ฐพ์";
});
*/
console.log(index); // 2
๐ callback ํจ์์ธ (item) => item === "๋ ์ฐพ์"
๊ฐ true๋ฅผ ๋ฐํํ๋ฉด ์์์ index์ธ 2๋ฅผ findIndex
ํจ์๊ฐ ๋ฐํํ๋ค.
const arr = [5, 6, 9, 1, 6, 3, 2, 1, 2, 7, 9, 4, 3];
const find = arr.findIndex((element, index, array) => {
return index < 7 && index > 5; // ๊ฒฐ๊ตญ index๊ฐ 6์ธ์ ์ฐพ๋๊ฑฐ์
});
console.log(find); // 6
์ฐธ๊ณ ๋งํฌ
https://bbaktaeho-95.tistory.com/40