Function countBy

  • Creates an object composed of keys generated from the results of running each element of collection thru iteratee. The corresponding value of each key is the number of times the key was returned by iteratee. The iteratee is invoked with one argument: (value).

    Type Parameters

    • T

    Parameters

    • collection: ArrayLike<T>

      The collection to iterate over.

    • iteratee: ArrayIteratee<T>

      The iteratee to transform keys.

    Returns Record<string, number>

    Returns the composed aggregate object.

    Since

    5.7.0

    Example

    const users = [
    { 'user': 'barney', 'active': true },
    { 'user': 'betty', 'active': true },
    { 'user': 'fred', 'active': false }
    ]

    countBy(users, value => value.active);
    // => { 'true': 2, 'false': 1 }
  • Type Parameters

    • T

    Parameters

    • collection: Record<string, T>
    • iteratee: RecordIteratee<T>

    Returns Record<string, number>