findIndex()
返回数组中满足提供的测试函数的第一个元素的索引。否则返回-1。
const array1 = [5, 12, 8, 130, 44]; const isLargeNumber = (element) => element > 13; console.log(array1.findIndex(isLargeNumber)); // expected output: 3
find()
返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined
。
const array1 = [5, 12, 8, 130, 44]; const found = array1.find(element => element > 10); console.log(found); // expected output: 12