JavaScript의 findIndex() 메소드는 조건이 전달되면 배열의 첫 번째 요소의 인덱스를 반환하는 데 사용됩니다.
구문은 다음과 같습니다 -
array.findIndex(function(currentValue, index, arr), thisValue)
이제 JavaScript에서 findIndex() 메서드를 구현해 보겠습니다. -
예시
<!DOCTYPE html>
<html>
<body>
<h2>Rank</h2>
<button onclick="display()">Result</button>
<p id="demo"></p>
<p>Finding the index of the player with highest rank.</p>
<script>
var points = [100, 150, 200, 250, 300, 400];
function topRank(points) {
return points >= 400;
}
function display() {
document.getElementById("demo").innerHTML = "index = "+points.findIndex(topRank);
}
</script>
</body>
출력

색인을 얻으려면 "결과"를 클릭하십시오 -

예시
<!DOCTYPE html>
<html>
<body>
<h2>Rank</h2>
<button onclick="display()">Result</button>
<p id="demo"></p>
<p>Finding the index of the player with specific rank points.</p>
<script>
var points = [100, 150, 200, 250, 300, 400];
function topRank(points) {
return points == 200;
}
function display() {
document.getElementById("demo").innerHTML = "index = "+points.findIndex(topRank);
}
</script>
</body>
출력

위의 "결과" 버튼을 클릭하십시오 -
