1.find()

find()方法返回数组中满足提供的测试函数的第一个元素**的值**。否则返回 undefined

  1. const array1 = [
  2. {id:1,value:12},
  3. {id:2,value:13},
  4. {id:3,value:14},
  5. {id:4,value:15},
  6. {id:5,value:16},
  7. {id:6,value:17},
  8. ];
  9. const found = array1.find(element => element.id == 4);
  10. console.log(found);
  11. // expected output: { id: 4, value: 15 }

2.findIndex()

findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引。否则返回 -1

  1. const array1 = [
  2. {id:1,value:12},
  3. {id:2,value:13},
  4. {id:3,value:14},
  5. {id:4,value:15},
  6. {id:5,value:16},
  7. {id:6,value:17},
  8. ];
  9. const found = array1.findIndex(element => element.id == 2);
  10. console.log(found);
  11. // expected output:1