Array.prototype.findLastIndex()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since â¨2022ë 8ìâ©.
Array ì¸ì¤í´ì¤ì findLastIndex() ë©ìëë ë°°ì´ì ììì¼ë¡ ìííë©° 주ì´ì§ íë³ í¨ì를 ë§ì¡±íë ë°°ì´ì 첫ë²ì§¸ ììì ì¸ë±ì¤ë¥¼ ë°íí©ëë¤.
ë§ì¡±íë ììê° ìì¼ë©´ -1ì ë°íí©ëë¤.
ì¸ë±ì¤ ëì íë³ í¨ì를 ë§ì¡±íë ë§ì§ë§ ê°ì ë°ííë findLast() ë©ìëë ì°¸ê³ íì¸ì.
ìëí´ ë³´ê¸°
const array1 = [5, 12, 50, 130, 44];
const isLargeNumber = (element) => element > 45;
console.log(array1.findLastIndex(isLargeNumber));
// Expected output: 3
// Index of element with value: 130
구문
findLastIndex(callbackFn)
findLastIndex(callbackFn, thisArg)
매ê°ë³ì
callbackFn-
ë°°ì´ì ê° ììì ëí´ ì¤íí í¨ìì ëë¤. ì´ í¨ìë 매ì¹ë ììê° ë°ê²¬ëììì ëíë´ê¸° ìí´ ì°¸ ê°ì ê°ì ë°íí´ì¼íë©°, ê·¸ë ì§ ìì¼ë©´ ê±°ì§ ê°ì ê°ì ë°íí´ì¼ í©ëë¤. ì´ í¨ìë ë¤ìê³¼ ê°ì ì¸ìì í¨ê» í¸ì¶ë©ëë¤.
thisArgOptional-
callbackFnì ì¤íí ëthisë¡ ì¬ì©í ê°ì²´ì ëë¤. ìì¸í ë´ì©ì ìí ê°ë¥ ë©ìë를 ì°¸ê³ íì기 ë°ëëë¤.
ë°í ê°
í
ì¤í¸ë¥¼ íµê³¼íë ë°°ì´ìì ë§ì§ë§ ìì(ê°ì¥ ëì ì¸ë±ì¤)ì ì¸ë±ì¤ì
ëë¤.
ë§ì½ ì¼ì¹íë ììê° ìë¤ë©´ -1ì ë°íí©ëë¤.
ì¤ëª
findLastIndex() ë©ìëë ìí ê°ë¥ ë©ìëì
ëë¤. ì´ ë©ìëë ë°°ì´ì ê° ììë§ë¤ ì ê³µë callbackFn í¨ì를 ììì¼ë¡ í ë²ì© í¸ì¶íë©°, callbackFnì´ ì°¸ ê°ì ê°ì ë°íí ëê¹ì§ ë°ë³µí©ëë¤. ì°¸ ê°ì ê°ì´ ë°íëë©´ findLastIndex()ë í´ë¹ ììì ì¸ë±ì¤ë¥¼ ë°ííê³ ë°°ì´ ìí를 ì¤ë¨í©ëë¤. ë§ì½ callbackFnì´ ì°¸ ê°ì ê°ì ë°ííì§ ìì¼ë©´, findLastIndex()ë -1ì ë°íí©ëë¤.
callbackFnë ë°°ì´ì 모ë ì¸ë±ì¤ì ëí´ í¸ì¶ë©ëë¤. í ë¹ë ê°ì´ ìë ì¸ë±ì¤ë¿ë§ ìëë¼ í¬ì ë°°ì´ì ë¹ì´ìë ì¬ë¡¯ë undefinedì ëì¼íê² ëìí©ëë¤.
findLastIndex() ë©ìëë ë²ì© ë©ìëì
ëë¤. this ê°ì length ìì±ê³¼ ì ì í¤ ìì± ë§ì 기ëí©ëë¤.
ìì
>ë°°ì´ìì ë§ì§ë§ ììì ì¸ë±ì¤ 찾기
ë¤ì ìì ë ë°°ì´ìì ë§ì§ë§ì¼ë¡ ëì¤ë ìì(prime number)ì ì¸ë±ì¤ë¥¼ ë°ííë©°, ììê° ìë ê²½ì° -1ì ë°íí©ëë¤.
function isPrime(element) {
if (element % 2 === 0 || element < 2) {
return false;
}
for (let factor = 3; factor <= Math.sqrt(element); factor += 2) {
if (element % factor === 0) {
return false;
}
}
return true;
}
console.log([4, 6, 8, 12].findLastIndex(isPrime)); // -1, ì°¾ì ì ìì
console.log([4, 5, 7, 8, 9, 11, 12].findLastIndex(isPrime)); // 5
callbackFnì ì¸ ë²ì§¸ ì¸ì ì¬ì©í기
array ì¸ìë ë°°ì´ì ë¤ë¥¸ ììì ì ê·¼íë ¤ë ê²½ì°, í¹í ë°°ì´ì 참조íë 기존 ë³ìê° ìë ê²½ì°ì ì ì©í©ëë¤. ë¤ì ìì ììë 먼ì filter()를 ì¬ì©íì¬ ìì ê°ì ì¶ì¶í ë¤ì findLastIndex()를 ì¬ì©íì¬ ì´ì ììë³´ë¤ ìì ë§ì§ë§ ìì를 ì°¾ìµëë¤.
const numbers = [3, -1, 1, 4, 1, 5, 9, 2, 6];
const lastTrough = numbers
.filter((num) => num > 0)
.findLastIndex((num, idx, arr) => {
// arr ì¸ìê° ìì¼ë©´ ë³ìë¡ ì ì¥íì§ ìê³ ì¤ê°ì ìì±ë
// ë°°ì´ì ì½ê² ì ê·¼í ì ìë ë°©ë²ì ììµëë¤.
if (idx > 0 && num >= arr[idx - 1]) return false;
if (idx < arr.length - 1 && num >= arr[idx + 1]) return false;
return true;
});
console.log(lastTrough); // 6
í¬ì ë°°ì´ìì findLastIndex() ì¬ì©
í¬ì ë°°ì´ìì undefined를 ì°¾ì ë¹ ì¬ë¡¯ì ì¸ë±ì¤ë¥¼ ììë¼ ì ììµëë¤.
console.log([1, , 3].findLastIndex((x) => x === undefined)); // 1
ë°°ì´ì´ ìë ê°ì²´ìì findLastIndex() ì¬ì©
findLastIndex() ë©ìëë thisì length ìì±ì ì½ì ë¤ì ê° ì ì ì¸ë±ì¤ì ì ê·¼í©ëë¤.
const arrayLike = {
length: 3,
0: 2,
1: 7.3,
2: 4,
};
console.log(
Array.prototype.findLastIndex.call(arrayLike, (x) => Number.isInteger(x)),
); // 2
ëª ì¸
| Specification |
|---|
| ECMAScript® 2026 Language Specification > # sec-array.prototype.findlastindex > |