标签:doc 目录 html turn undefined ges 测试 tle 目标
find()函数用来查找目标元素,找到就返回该元素,找不到返回undefined。
<p id="demo"></p><!--显示18-->
<button onclick="myFunction()">click</button>
<script>
var ages=[3,10,18,20];
function myFunction(){
document.getElementById("demo").innerHTML=ages.find((item,index,array)=>{
return item>=18;
});
}
</script>
findIndex() 方法返回传入一个测试条件(函数)符合条件的数组第一个元素位置。
find为数组中每一个元素都调用一次函数执行:
<p>点击按钮获取数组中年龄大于 18 的第一个元素索引位置。</p>
<button onclick="myFunction()">点我</button>
<p id="demo"></p><!--显示2-->
<p><strong>注意:</strong> IE 11 及更早版本不支持 findIndex() 方法。</p>
<script>
var ages = [3, 10, 18, 20];
function myFunction(){
document.getElementById("demo").innerHTML=ages.findIndex((item,index,array)=>{
return item>=18;
})
}
//array.findIndex(function(item,index,array))
</script>
标签:doc 目录 html turn undefined ges 测试 tle 目标
原文地址:https://www.cnblogs.com/Syinho/p/12404082.html