BaseFindIndex

Published: · LastMod: September 28, 2021 · 64 words

baseFindIndex 🔗

查找数组中的索引

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
function baseFindIndex(array, predicate, fromIndex, fromRight) {
  const { length } = array
  let index = fromIndex + (fromRight ? 1 : -1)

  while ((fromRight ? index-- : ++index < length)) {
    if (predicate(array[index], index, array)) {
      return index
    }
  }
  return -1
}