代码编辑器:
x
1
<p>点击按钮获取数组中年龄大于 18 的第一个元素索引位置</p>
2
<button onclick="myFunction()">点我</button>
3
<p id="demo"></p>
4
<p><strong>注意:</strong> IE 11 及更早版本不支持 findIndex() 方法</p>
5
<script>
6
var ages = [3, 10, 18, 20];
7
function checkAdult(age) {
8
return age >= 18;
9
}
10
function myFunction() {
11
document.getElementById("demo").innerHTML = ages.findIndex(checkAdult);
12
}
13
</script>
14