• Array.prototype.indexOf() 判断数组中是否存在某个值,如果存在,则返回数组元素的下标,否则返回 -1
const arr = ['red', 'yellow', 'black', 'white', 'yellow']

arr.indexOf('plum') // -1
arr.indexOf('yellow') // 1
arr.indexOf('yellow', 2) // 4

if (arr.indexOf('red') != -1) {
  console.log('存在')
} // 存在
arr.includes('red') // true
arr.includes('plum') // false

if (arr.includes('red')) {
  console.log('存在')
} // 存在
  • Array.prototype.find() 返回数组中满足条件的第一个元素的值,如果没有,返回 undefined
arr.find(item => {
  if (item === 'black') return item
}) // "black"
arr.findIndex(item => item === 'white') // 3