Skip to main content

Array grouping

Less than 1 minutefeaturees-proposal

Array groupingopen in new window

Modules

Types

interface ObjectConstructor {
  groupBy<T>(
    items: Iterable<T>,
    callbackfn: (value: T, index: number) => string | number
  ): { [index: string]: Array<T> };
}
interface MapConstructor {
  groupBy<K, V>(
    items: Iterable<V>,
    callbackfn: (value: V, index: number) => K
  ): Map<K, Array<V>>;
}

Entry points

core-js/proposals/array-grouping-v2
core-js(-pure)/full/map/group-by
core-js(-pure)/full/object/group-by

Example

Exampleopen in new window:

Object.groupBy([1, 2, 3, 4, 5], (it) => it % 2); // => { 1: [1, 3, 5], 0: [2, 4] }
const map = Map.groupBy([1, 2, 3, 4, 5], (it) => it % 2);
map.get(1); // => [1, 3, 5]
map.get(0); // => [2, 4]