Skip to main content

type groupBy

Returns a Reducer that uses the valueToKey function to calculate a key for each value, and feeds the tuple of the key and the value to the collector reducer. Finally, it returns the output of the collector. If no collector is given, the default collector will return a JS multimap of the type Map<K, V[]>.

example
Stream.of(1, 2, 3).groupBy((v) => v % 2)
// => Map {0 => [2], 1 => [1, 3]}

Definition

groupBy: {
    <T, K, R, T2 extends readonly [K, T] = [K, T]>(valueToKey: (value: T, index: number) => K, options: {
      collector: Reducer<[K, T] | T2, R>;
    }): Reducer<T, R>;
    <T, K>(valueToKey: (value: T, index: number) => K, options?: {
      collector?: undefined;
    }): Reducer<T, Map<K, T[]>>;
  }