@pengzhanbo/utils - v3.4.1
    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 keys in the object. 对象中键的类型

      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 Map for O(1) key lookup, overall O(n) time complexity

      使用 Map 实现 O(1) 键查找,整体时间复杂度 O(n)

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