Function group

  • Groups the passed-in iterable by the given key.

    Type Parameters

    • Iter extends AnyIt<unknown>
    • T = AnyItV<Iter>

    Parameters

    • iter: Iter

      A sync or async iterable.

    • Optional key: ValFunc<AnyItV<Iter>, T>

      A function computing the key, such as item => item['foo']. Key defaults to an identity function, grouping duplicated values.

    Returns AnyItResult<Iter, Map<T, AnyItV<Iter>[]>>

    A Map where each key is a group.

    Example

    // Returns Map(1: [1, 1], 2: [2, 2], 3: [3])
    it.group([1, 1, 2, 2, 3])

    Example

    // Returns Map(true: [1, 1], false: [2, 2, 3])
    it.group([1, 1, 2, 2, 3], x => x < 2)

    Example

    // Returns Map(1: [{id: 1}], 2: [{id: 2}])
    it.group([{id: 1}, {id: 2}], x => x.id)