Kwhitejr Kwhitejr - 1 year ago 70
Javascript Question

How to reduce an array of objects on multiple variables in Javascript?

I would like to know how to reduce a large array of objects based upon several properties. The array looks like:

[{count:4, district:19, to_timestamp:"2015-09-24T00:00:00.000Z", type:"MOTOR VEHICLE THEFT"},
{count:6, district:12, to_timestamp:"2015-09-24T00:00:00.000Z", type:"MOTOR VEHICLE THEFT"},
{count:14, district:19, to_timestamp:"2015-10-01T00:00:00.000Z", type:"MOTOR VEHICLE THEFT"},
{count:4, district:19, to_timestamp:"2015-09-24T00:00:00.000Z", type:"VANDALISM"},
{count:4, district:19, to_timestamp:"2016-03-24T00:00:00.000Z", type:"MOTOR VEHICLE THEFT"},
{count:7, district:10, to_timestamp:"2016-03-24T00:00:00.000Z", type:"ASSAULT"}]

Resulting reduced array of objects needs to include the aggregate
from every
for given crime
on any given date (
); dates are weekly. Something like:

[{key: "MOTOR VEHICLE THEFT", value: 57, date:"2015/09/23"},
{key: "ASSAULT", value: 77, date:"2016/03/23"}]

I'm already using Moment for date conversions.

Answer Source

You could use an object as hash table for the wanted groups (type and to_timestamp) and use Array#forEach for iterating the array.

var data = [{ count: 4, district: 19, to_timestamp: "2015-09-24T00:00:00.000Z", type: "MOTOR VEHICLE THEFT" }, { count: 6, district: 12, to_timestamp: "2015-09-24T00:00:00.000Z", type: "MOTOR VEHICLE THEFT" }, { count: 14, district: 19, to_timestamp: "2015-10-01T00:00:00.000Z", type: "MOTOR VEHICLE THEFT" }, { count: 4, district: 19, to_timestamp: "2015-09-24T00:00:00.000Z", type: "VANDALISM" }, { count: 4, district: 19, to_timestamp: "2016-03-24T00:00:00.000Z", type: "MOTOR VEHICLE THEFT" }, { count: 7, district: 10, to_timestamp: "2016-03-24T00:00:00.000Z", type: "ASSAULT" }],
    groupBy = ['type', 'to_timestamp'],
    grouped = [];

data.forEach(function (a) {
    var key = (k) { return a[k]; }).join('|');
    if (!this[key]) {
        this[key] = { key: a.type, value: 0, date: a.to_timestamp.slice(0, 10).replace(/-/g, '/') };
    this[key].value += a.count;
}, Object.create(null));


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download