Published
Edited
Sep 10, 2018
Fork of Group
1 star
Insert cell
Insert cell
Insert cell
function groupReduce(entries, entryof = identity, reduce) { // no `init` argument
const map = new Map();
let index = -1;
for (const entry of entries) {
const [key, value] = entryof(entry, ++index, entries);
// if map[key] isn't initialized, pass undefined to the reducer
map.set(key, reduce(map.has(key) ? map.get(key) : undefined, value, index, entries));
}
return map;
}
Insert cell
Insert cell
group = {
const entryIdentity = value => [value, value];
const entryKey = keyof => (value, index, values) => [keyof(value, index, values), value];
const arrayReduce = (p = [] /* default argument is used for initialization */, v) => (p.push(v), p);
// no need for arrayInit anymore
return function group(values, keyof) {
return groupReduce(
values,
keyof === undefined ? entryIdentity : entryKey(keyof),
arrayReduce
);
};
}
Insert cell
Insert cell
group(data, d => d.name)
Insert cell
Insert cell
group([1, 1, 1, 2, 3, 4, 4, 2], d => d & 1)
Insert cell
group([1, 1, 1, 2, 3, 4, 4, 2])
Insert cell
groupMap(data, d => [d.name, d.value])
Insert cell
Insert cell
groupSum(data, d => [d.name, d.value])
Insert cell
Insert cell
groupMin(data, d => [d.name, d.value])
Insert cell
Insert cell
groupMax(data, d => [d.name, d.value])
Insert cell
Insert cell
groupCount(data, d => d.date.toISOString())
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more