Function groupByRecord

  • Groups the source by a key selector.

    Type Parameters

    • T

      Type of items in the source.

    • TKey extends string | number | symbol

      Type of the key.

    Parameters

    • keySelector: ((entry: T) => TKey)

      Key selector function.

    Returns CollectorFunction<T, Record<TKey, T[]>>

    Collector that groups the source by a key selector.

    const result = groupByRecord<number, string>((x) => x % 2 === 0 ? "even" : "odd")([1, 2, 3, 4]);
    console.log(result);
    // {
    // 'odd': [1, 3],
    // 'even': [2, 4],
    // }