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

    Function countBy

    • Counts the occurrences of each value returned by the iteratee function.

      This function takes an array and an iteratee function, and returns an object where each key is a value returned by the iteratee function, and the value is the count of elements that produced that key.

      统计 iteratee 函数返回的每个值的出现次数。

      此函数接收一个数组和一个 iteratee 函数,返回一个对象,其中每个键是 iteratee 函数返回的值, 值是产生该键的元素数量。

      Type Parameters

      • T

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

      • K extends PropertyKey

        The type of the key (extends PropertyKey) / 键的类型(继承 PropertyKey)

      Parameters

      • array: readonly T[]

        The array to count. 要统计的数组

      • iteratee: (item: T) => K

        The function to transform elements into keys. 将元素转换为键的函数

      Returns Record<K, number>

      An object with keys mapped to their counts. 键映射到其计数的对象

      Uses plain object for key lookup, overall O(n) time complexity. Key lookup is O(1) via hasOwn.

      使用普通对象实现键查找,整体时间复杂度 O(n)。 通过 hasOwn 实现 O(1) 键查找。

      countBy(['one', 'two', 'three'], x => x.length)
      // => { 3: 2, 5: 1 }
      countBy([6.1, 4.2, 6.3], Math.floor)
      // => { 6: 2, 4: 1 }