@vsokolov/utils - v1.4.2
    Preparing search index...

    Function groupBy

    • Group array items by a key extractor function

      Type Parameters

      • T
      • K extends string | number | symbol

      Parameters

      • arr: readonly T[]

        Array to group

      • getKey: (item: T) => K

        Function to extract group key from each item

      Returns Record<K, T[]>

      Record of arrays grouped by key

      groupBy([{id: 1, type: 'a'}, {id: 2, type: 'b'}, {id: 3, type: 'a'}], item => item.type)
      // { a: [{id: 1, type: 'a'}, {id: 3, type: 'a'}], b: [{id: 2, type: 'b'}] }