@eroyeroy/utils - v0.1.0
    Preparing search index...

    Function countBy

    • Counts the occurrences of each distinct value of key across the array. Items where the key value is not a valid property key are skipped.

      Type Parameters

      • T
      • K extends string | number | symbol

      Parameters

      • array: readonly T[]

        The array to count.

      • key: K

        The key whose values are used as group identifiers.

      Returns Record<Extract<T[K], PropertyKeyValue>, number>

      countBy(
      [{ role: "admin" }, { role: "user" }, { role: "admin" }],
      "role"
      )
      // => { admin: 2, user: 1 }