运用findIndex获取元素的索引

点击按钮返回符合大于输入框中指定数字的数组元素索引。
最小年龄: <input type="number" id="ageToCheck" value="18">
<button onclick="myFunction()">点我</button>

索引: <span id="demo"></span><

注意: IE 11 及更早版本不支持 findIndex() 方法。

var ages = [{a:4}, {a:12}, {a:16}, {a:20}];

function checkAdult(age,index) {
return age.a >= document.getElementById("ageToCheck").value;
}

function myFunction() {
document.getElementById("demo").innerHTML = ages.findIndex(checkAdult);
}
原文地址:https://www.cnblogs.com/lezuw/p/11340204.html