Function groupBy

Groups data by a function grouper, returning data as a map with string keys and array values. Multiple values can be assigned to the same group.

grouper must yield a string designated group for a given item.

import { Arrays } from 'https://unpkg.com/ixfx/dist/data.js';

const data = [
{ age: 39, city: `London` },
{ age: 14, city: `Copenhagen` },
{ age: 23, city: `Stockholm` },
{ age: 56, city: `London` }
];

// Whatever the function returns will be the designated group
// for an item
const map = Arrays.groupBy(data, item => item.city);

This yields a Map with keys London, Stockholm and Copenhagen, and the corresponding values.

London: [{ age: 39, city: `London` }, { age: 56, city: `London` }]
Stockhom: [{ age: 23, city: `Stockholm` }]
Copenhagen: [{ age: 14, city: `Copenhagen` }]
  • Type Parameters

    • K

      Type of key to group by. Typically string.

    • V

      Type of values

    Parameters

    • array: Iterable<V>

      Array to group

    • grouper: ((item: V) => K)

      Function that returns a key for a given item

        • (item): K
        • Parameters

          • item: V

          Returns K

    Returns Map<K, V[]>

    Map