JS查找数组中元素index

findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回-1。

 

值类型:   

const array1 = [5, 12, 8, 130, 44];

const isLargeNumber = (element) => element == 12;

console.log(array1.findIndex(isLargeNumber));

 

数组对象:   var index = DiaryNoList.findIndex((value) => value.diaryno == this.info.diaryno);

 

 

find()

const array1 = [5, 12, 8, 130, 44];

const found = array1.find(element => element > 10);

console.log(found);
// expected output: 12

posted @ 2021-09-10 18:04  越过那个限制  阅读(2516)  评论(0编辑  收藏  举报