Published
Edited
Nov 17, 2018
Importers
Insert cell
Insert cell
Insert cell
Insert cell
dataByGroup = {
let result = [];
data.reduce(function (res, value) {
if (!res[value.group]) {
res[value.group] = {
value: 0,
group: value.group
};
result.push(res[value.group])
}
res[value.group].value += value.value
return res;
}, {});
return result;
}
Insert cell
Insert cell
Insert cell
Insert cell
dataByGroupsAndMonths = {
let result = [];
data.reduce(function (res, value) {
const month = new Date(value.date.getFullYear(), value.date.getMonth(), 1);
const key = JSON.stringify({group: value.group, month: month});
if (!res[key]) {
res[key] = {
group: value.group,
date: month,
value: 0
};
result.push(res[key]);
}
res[key].value += value.value
return res;
}, {})
return result;
}
Insert cell
data.reduce(function (res, value) {
if (!res[{group: value.group, month: new Date(value.date.getFullYear(), value.date.getMonth(), 1)}]) {
res[{group: value.group, month: new Date(value.date.getFullYear(), value.date.getMonth(), 1)}] = {
group: value.group,
month: new Date(value.date.getFullYear(), value.date.getMonth(), 1),
value: 0
};
}
res[{group: value.group, month: new Date(value.date.getFullYear(), value.date.getMonth(), 1)}].value += value.value
return res;
}, {})
Insert cell
groupBy = function(xs, key) {
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
}
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