# minBy

> "Let go of the thoughts that don't make you strong." —Unknown

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

## 依赖

```javascript
import isSymbol from './isSymbol.js';
```

* [lodash 源码阅读 —— isSymbol](/lodash-analysis/lang/issymbol.md)

## 源码

```javascript
/**
 * 这个方法类似 _.min(已弃用), 计算 array 中的最小值， 如果 array 是 空的或者假值将会返回 undefined
 * 但它接受 iteratee 来调用 array 中的每一个元素，来生成其值排序的标准。
 * iteratee 会调用1个参数: (value) 。
 *
 * @since 4.0.0
 * @category Math
 * @param {Array} array 要迭代的数组。
 * @param {Function} iteratee 调用每个元素的迭代函数。
 * @returns {*} 返回最小的值。
 * @example
 *
 * const objects = [{ 'n': 1 }, { 'n': 2 }]
 *
 * minBy(objects, ({ n }) => n)
 * // => { 'n': 1 }
 */
function minBy(array, iteratee) {
  let result;
  if (array == null) {
    return result;
  }
  let computed;
  for (const value of array) {
    const current = iteratee(value);

    if (current != null && (computed === undefined ? current === current && !isSymbol(current) : current < computed)) {
      computed = current;
      result = value;
    }
  }
  return result;
}
```

## 原理

`minBy` 使用 `for...of` 遍历 `array`，这样做的好处是可以兼容自定义 `Symbol.iterator` 的 `array` 参数以及更多的类型（例如: `String/TypeArray/Map/Set/arguments对象/DOM集合`等）；再通过传入的 `iteratee` 方法取得当前值 `current`，通过对 `current` 的类型判断，排除 `null/undefinded/NaN/Symbol` 情况，与缓存变量 `computed` 比对大小，最终计算返回正确的最大值结果。

## Fix

阅读版本代码存在错误，已提 [PR](https://github.com/lodash/lodash/pull/4233) 进行修复.

## 相关链接

* [MDN - for...of](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/for...of)
* [lodash 源码阅读 —— isSymbol](/lodash-analysis/lang/issymbol.md)


---

# 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/math/minby.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.
