@trashpanda001/helpers
    Preparing search index...

    Function groupBy

    • Splits an array into groups based on keyFn.

      The result is an object where each key is given by keyFn and each value is an array of elements given by valueFn. The order of elements within each list is preserved from the original array.

      Type Parameters

      • T
      • K extends PropertyKey
      • V = T

      Parameters

      • array: readonly T[]

        the array to group

      • keyFn: (x: T) => K

        a function that returns the key for each element

      • valueFn: (x: T) => V = ...

        a function that returns the value for each element

      Returns Record<K, V[]>

      an object that maps group keys to arrays of elements/values

      Group elements by their string length.

      import { groupBy } from "@trashpanda001/helpers/array"

      groupBy(["one", "two", "three", "four", "five"], (x) => x.length)
      // { "3": ["one", "two"], "4": ["four", "five"], "5": ["three"] }
      groupBy(["one", "two", "three", "four", "five"], (x) => x.length, (x) => x.toUpperCase())
      // { "3": ["ONE", "TWO"], "4": ["FOUR", "FIVE"], "5": ["THREE"] }