findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回-1。
例子:
const array1 = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => element > 13;
console.log(array1.findIndex(isLargeNumber));
// expected output: 3
实战:
array.findIndex(是否包含这一项)
返回结果:-1(没有这一项)
返回结果:1(有这一项)
实操:
newarray.findIndex(num)
console.log(查看返回结果)
根据返回结果:做出逻辑处理或逻辑判断等…
var busID=res.data.id;
//this.statdata:是这个数组
//busID:是个变量 (接收的是做对比的项):可以自己定义接收的变量
this.statdata=res.data.businessTypes;
function isBigEnough(element,index,array) {
return element == busID;//做对比的id//element=赋值当前作对比的项
}
this.statdata.findIndex(isBigEnough)
console.log(this.statdata.findIndex(isBigEnough))//查看返回结果