Baseline Widely available
Array
ì¸ì¤í´ì¤ì findIndex()
ë©ìëë 주ì´ì§ íë³ í¨ì를 ë§ì¡±íë ë°°ì´ì 첫 ë²ì§¸ ììì ëí ì¸ë±ì¤ë¥¼ ë°íí©ëë¤. ë§ì¡±íë ììê° ìì¼ë©´ -1ì ë°íí©ëë¤.
íë³ í¨ì를 ë§ì¡±íë 첫ë²ì§¸ ì¸ë±ì¤ ëì ê°ì ë°ííë find()
ë©ìëë ì°¸ê³ íì기 ë°ëëë¤.
const array1 = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => element > 13;
console.log(array1.findIndex(isLargeNumber));
// Expected output: 3
구문
findIndex(callbackFn)
findIndex(callbackFn, thisArg)
매ê°ë³ì
callbackFn
ë°°ì´ì ê° ììë§ë¤ ì¤íí í¨ìì ëë¤. ì¼ì¹íë ììê° ë°ê²¬ëììì ëíë´ë ì°¸ ê°ì ê°ì ë°ííê³ ê·¸ë ì§ ìì¼ë©´ ê±°ì§ê°ì ê°ì ë°íí´ì¼í©ëë¤. í¨ìë ë¤ìê³¼ ê°ì ì¸ìì í¨ê» í¸ì¶ë©ëë¤.
element
ë°°ì´ìì ì²ë¦¬ ì¤ì¸ íì¬ ììì ëë¤.
index
ë°°ì´ìì ì²ë¦¬ ì¤ì¸ íì¬ ììì ì¸ë±ì¤ì ëë¤.
array
findIndex()
í¨ìê° í¸ì¶ë ë°°ì´ì
ëë¤.
thisArg
Optional
callbackFn
ì ì¤íí ë this
ë¡ ì¬ì©í ê°ì
ëë¤. ìí ë©ìë를 ì°¸ê³ íì¸ì.
í
ì¤í¸ë¥¼ íµê³¼íë 첫 ë²ì§¸ ììì ì¸ë±ì¤ì
ëë¤. ì¼ì¹íë ììê° ìì¼ë©´ -1
ì ë°íí©ëë¤.
findIndex()
ë ìí ë©ìëì
ëë¤. callbackFn
í¨ì를 ë°°ì´ì ê° ììì ëí´ ì¤ë¦ì°¨ì ì¸ë±ì¤ ììë¡ í ë²ì© í¸ì¶íê³ , callbackFn
ì´ ì°¸ ê°ì ê°ì ë°íí ëê¹ì§ ë°ë³µí©ëë¤. ê·¸ë° ë¤ì findIndex()
ë í´ë¹ ììì ì¸ë±ì¤ë¥¼ ë°ííê³ ë°°ì´ ë°ë³µì ì¤ë¨í©ëë¤. callbackFn
ì´ ì°¸ ê°ì ê°ì ë°ííì§ ìì¼ë©´ findIndex()
ë -1
ì ë°íí©ëë¤. ë³´í¸ì ì¼ë¡ ì´ë¤ ë©ìëê° ëìíë ë°©ë²ì ìê³ ì¶ì¼ìë¤ë©´ ìí ë©ìë ì¹ì
ì ì½ì´ë³´ì기 ë°ëëë¤.
callbackFn
ì ë°°ì´ì ê°ì´ í ë¹ë ì¸ë±ì¤ ë¿ë§ì´ ìë 모ë ì¸ë±ì¤ì ëí´ í¸ì¶ë©ëë¤. í¬ì ë°°ì´ì ë¹ ì¬ë¡¯ì undefined
ì ëì¼íê² ëìí©ëë¤.
findIndex()
ë©ìëë ë²ì©ë©ìë ì
ëë¤. this
ê°ì length
ìì±ê³¼ ì ì í¤ ìì± ë§ì 기ëí©ëë¤.
ë¤ì ìì ììë ë°°ì´ìì ììì¸ ì²«ë²ì§¸ ììì ì¸ë±ì¤ë¥¼ ì°¾ìµëë¤. ììê° ìì¼ë©´ -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, 9, 12].findIndex(isPrime)); // -1, ì°¾ì ì ìì
console.log([4, 6, 7, 9, 12].findIndex(isPrime)); // 2 (array[2] is 7)
callbackFnì ì¸ ë²ì§¸ ì¸ì ì¬ì©í기
array
ì¸ìë ë°°ì´ì ë¤ë¥¸ ììì ì ê·¼íë ¤ë ê²½ì°, í¹í ë°°ì´ì 참조íë 기존 ë³ìê° ìë ê²½ì°ì ì ì©í©ëë¤. ë¤ì ìì ììë 먼ì filter()
를 ì¬ì©íì¬ ìì ê°ì ì¶ì¶í ë¤ì findIndex()
를 ì¬ì©íì¬ ì´ì ììë³´ë¤ ìì 첫 ë²ì§¸ ìì를 ì°¾ìµëë¤.
const numbers = [3, -1, 1, 4, 1, 5, 9, 2, 6];
const firstTrough = numbers
.filter((num) => num > 0)
.findIndex((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(firstTrough); // 1
í¬ì ë°°ì´ìì findIndex() ì¬ì©í기
í¬ì ë°°ì´ìì undefined
를 ê²ìíê³ ë¹ ì¬ë¡¯ì ì¸ë±ì¤ë¥¼ ì»ì ì ììµëë¤.
console.log([1, , 3].findIndex((x) => x === undefined)); // 1
ë°°ì´ì´ ìë ê°ì²´ì findIndex() í¸ì¶í기
findIndex()
ë this
ì length
ìì±ì ì½ì ë¤ì ììê° ìëë©´ì length
ë³´ë¤ ìì ê° ì ì ìì±ì ì ê·¼í©ëë¤.
const arrayLike = {
length: 3,
"-1": 0.1, // -1 < 0 ì´ê¸° ë문ì findIndex() ê° ì´ë¥¼ 무ìí©ëë¤.
0: 2,
1: 7.3,
2: 4,
};
console.log(
Array.prototype.findIndex.call(arrayLike, (x) => !Number.isInteger(x)),
); // 1
ëª
ì¸ì ë¸ë¼ì°ì í¸íì± ê°ì´ 보기
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4