1.find()
find()方法返回数组中满足提供的测试函数的第一个元素**的值**。否则返回 undefined
const array1 = [
{id:1,value:12},
{id:2,value:13},
{id:3,value:14},
{id:4,value:15},
{id:5,value:16},
{id:6,value:17},
];
const found = array1.find(element => element.id == 4);
console.log(found);
// expected output: { id: 4, value: 15 }
2.findIndex()
findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引。否则返回 -1
。
const array1 = [
{id:1,value:12},
{id:2,value:13},
{id:3,value:14},
{id:4,value:15},
{id:5,value:16},
{id:6,value:17},
];
const found = array1.findIndex(element => element.id == 2);
console.log(found);
// expected output:1