I'm not going to dwell too long on d3-collection, as Babel provides objects, such as Maps and Sets, as part of the native JavaScript API. If you're developing for earlier browsers and not using Babel to transpile your code, d3-collection provides a good path to use some modern idioms without having to polyfill a bunch of stuff. All that said, it does have one incredibly useful utility, which is d3.nest. A nest is a structure similar to a NoSQL query result, in which documents have been aggregated into a group.
Say you get the following from a CSV document:
const sensorData = [
{location: "a", status: "normal", average: "100", date: "2016-11-01"},
{location: "a", status: "normal", average: "200", date: "2016-11-02"},
{location: "a", status: "normal", average: "300", date: "2016-11-03"},
[...]
{location: "b", status: "normal", average: "400", date: "2016-11-01"},
{location: "b", status: "alarm", average: "500", date: "2016-11-02"},
{location: "b", status: "alarm", average: "600", date: "2016-11-03"},
[...]
];
This might be from a spreadsheet that's a dump of a MySQL database somewhere, with a header row containing a location, an average value from a sensor, and the date on which the reading was taken. We can get by with a few complex Map.prototype.reduce calls or simply assign them to a nest and then roll it up, as follows:
d3.nest()
.key(d => d.location)
.rollup(d => d3.mean(d, v => v.average))
.map(sensorData);
// >> {a: 200, b: 500}
What's happening here? We first create a map, which is an object that assigns one or more values to a particular key. In this case, we've chosen to group by the location. Then, we roll up the values, meaning that we run a function that assigns a value to the key -- in this case, we get the average of all items in each day (d, in this case, is an array containing all values associated with a key).