1.用法概述
findIndex( )返回数组中满足回调函数测试条件的第一个元素的索引位置,返回值是Number,若不存在则返回-1
2.语法
arr.findIndex(callbackFun( item, index, arr ) { })
或arr.findIndex( ( item, index, arr ) => { })
1)参数
callbackFun()——数组中每个元素执行的函数,该函数有三个参数。
item——当前遍历到的数组元素值。
index——当前遍历到的数组元素的下标。
arr——调用findIndex()方法的数组
2)返回值
返回满足回调函数测试条件的第一个元素的索引位置,若不存在则返回-1
3.案例用法
1、查找数组中首个元素的索引位置
const s1 = [2,5,3,7,12,18];
const s2 = s1.findIndex((item,index,arr)=>{
return item > 7
});
console.log(s2); //4