# baseFindIndex

> "It is our choices that show what we truly are, far more than our abilities." —@@jk\_rowling

本文为 《lodash 源码阅读》 系列文章，后续内容会在 [github](https://github.com/gu-xionghong/lodash-analysis) 中发布，欢迎 star，[gitbook](https://gu-xionghong.gitbook.io/lodash-analysis/) 同步更新。

## 源码

```javascript
/**
 * `findIndex` 与 `findLastIndex` 的基础实现。
 *
 * @private
 * @param {Array} array 要搜索的数组。
 * @param {Function} predicate 每次迭代调用的函数。
 * @param {number} fromIndex 开始搜索的索引位置。
 * @param {boolean} [fromRight] 制定遍历顺序是否从右往左。
 * @returns {number} 返回找到元素的 索引值（index），否则返回 -1。
 */
function baseFindIndex(array, predicate, fromIndex, fromRight) {
  const { length } = array;
  let index = fromIndex + (fromRight ? 1 : -1);

  // 遍历迭代 array
  while (fromRight ? index-- : ++index < length) {
    if (predicate(array[index], index, array)) {
      return index;
    }
  }
  return -1;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gu-xionghong.gitbook.io/lodash-analysis/.internal/basefindindex.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
