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
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 itemconst map = Arrays.groupBy(data, item => item.city); Copy
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 itemconst 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` }] Copy
London: [{ age: 39, city: `London` }, { age: 56, city: `London` }]Stockhom: [{ age: 23, city: `Stockholm` }]Copenhagen: [{ age: 14, city: `Copenhagen` }]
Type of key to group by. Typically string.
Type of values
Array to group
Function that returns a key for a given item
Map
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.Example
This yields a Map with keys London, Stockholm and Copenhagen, and the corresponding values.