@pengzhanbo/utils - v3.9.0
    Preparing search index...

    Function minBy

    • Returns the first element of an array with the minimum value according to an iteratee function.

      The iteratee is invoked for each element, and the element with the smallest resulting value is returned. If multiple elements tie for the minimum, the first one encountered is kept. If the array is empty, undefined is returned.

      根据迭代函数返回数组中对应值最小的第一个元素。

      对每个元素调用迭代函数,返回结果值最小的那个元素。若多个元素并列最小,则保留最先遇到的元素。若数组为空,返回 undefined

      Type Parameters

      • T

        The type of elements in the array / 数组元素的类型

      Parameters

      • arr: readonly T[]

        The array to search. 要搜索的数组

      • iteratee: (item: T) => string | number

        The function invoked per element, returning a number or string. 对每个元素调用的迭代函数,返回数字或字符串

      Returns T | undefined

      The element with the minimum value, or undefined if the array is empty. 值最小的元素,数组为空时返回 undefined

      O(n) time complexity - single pass through the array

      O(n) 时间复杂度 - 单次遍历数组

      minBy([{ n: 1 }, { n: 3 }, { n: 2 }], (u) => u.n)
      // => { n: 1 }
      minBy(['pear', 'apple', 'banana'], (s) => s)
      // => 'apple'