Reputation: 1813
I have an array of objects that look as below
[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
There are several hundred objects in the array. Expanding a few of them below -
0
:
Object
EVENT_TYPE_1:3
EVENT_TYPE_2:0
EVENT_TYPE_3:2
EVENT_TYPE_4:6
EVENT_TYPE_5:
EVENT_TYPE_6:0
EVENT_TYPE_7:0
EVENT_TYPE_8:26
EVENT_DATE:"2016-05-05"
1
:
Object
EVENT_TYPE_1:3
EVENT_TYPE_2:0
EVENT_TYPE_3:2
EVENT_TYPE_4:6
EVENT_TYPE_5:15
EVENT_TYPE_6:43
EVENT_TYPE_7:0
EVENT_TYPE_8:26
EVENT_DATE:"2016-05-06"
2
:
Object
EVENT_TYPE_1:30
EVENT_TYPE_2:0
EVENT_TYPE_3:20
EVENT_TYPE_4:6
EVENT_TYPE_5:1
EVENT_TYPE_6:0
EVENT_TYPE_7:10
EVENT_TYPE_8:26
EVENT_DATE:"2016-05-07"
3
:
Object
EVENT_TYPE_1:3
EVENT_TYPE_2:0
EVENT_TYPE_3:27
EVENT_TYPE_4:6
EVENT_TYPE_5:154
EVENT_TYPE_6:0
EVENT_TYPE_7:87
EVENT_TYPE_8:45
EVENT_DATE:"2016-05-08"
I'd like to add up the values for each EVENT_TYPE for the days of the week & generate an output as below -
[
{EVENT_DATE:SUN,EVENT_TYPE_1:<sum of all EVENT_TYPE_1 for SUN}
{EVENT_DATE:MON,EVENT_TYPE_1:<sum of all EVENT_TYPE_1 for MON}
{EVENT_DATE:TUE,EVENT_TYPE_1:<sum of all EVENT_TYPE_1 for TUE}
{EVENT_DATE:WED,EVENT_TYPE_1:<sum of all EVENT_TYPE_1 for WED}
{EVENT_DATE:THU,EVENT_TYPE_1:<sum of all EVENT_TYPE_1 for THU}
{EVENT_DATE:FRI,EVENT_TYPE_1:<sum of all EVENT_TYPE_1 for FRI}
{EVENT_DATE:SAT,EVENT_TYPE_1:<sum of all EVENT_TYPE_1 for SAT}
{EVENT_DATE:SUN,EVENT_TYPE_2:<sum of all EVENT_TYPE_2 for SUN}
{EVENT_DATE:MON,EVENT_TYPE_2:<sum of all EVENT_TYPE_2 for MON}
{EVENT_DATE:TUE,EVENT_TYPE_2:<sum of all EVENT_TYPE_2 for TUE}
{EVENT_DATE:WED,EVENT_TYPE_2:<sum of all EVENT_TYPE_2 for WED}
{EVENT_DATE:THU,EVENT_TYPE_2:<sum of all EVENT_TYPE_2 for THU}
{EVENT_DATE:FRI,EVENT_TYPE_2:<sum of all EVENT_TYPE_2 for FRI}
{EVENT_DATE:SAT,EVENT_TYPE_2:<sum of all EVENT_TYPE_2 for SAT}
{EVENT_DATE:SUN,EVENT_TYPE_3:<sum of all EVENT_TYPE_3 for SUN}
{EVENT_DATE:MON,EVENT_TYPE_3:<sum of all EVENT_TYPE_3 for MON}
{EVENT_DATE:TUE,EVENT_TYPE_3:<sum of all EVENT_TYPE_3 for TUE}
{EVENT_DATE:WED,EVENT_TYPE_3:<sum of all EVENT_TYPE_3 for WED}
{EVENT_DATE:THU,EVENT_TYPE_3:<sum of all EVENT_TYPE_3 for THU}
{EVENT_DATE:FRI,EVENT_TYPE_3:<sum of all EVENT_TYPE_3 for FRI}
{EVENT_DATE:SAT,EVENT_TYPE_3:<sum of all EVENT_TYPE_3 for SAT}
{EVENT_DATE:SUN,EVENT_TYPE_4:<sum of all EVENT_TYPE_4 for SUN}
{EVENT_DATE:MON,EVENT_TYPE_4:<sum of all EVENT_TYPE_4 for MON}
{EVENT_DATE:TUE,EVENT_TYPE_4:<sum of all EVENT_TYPE_4 for TUE}
{EVENT_DATE:WED,EVENT_TYPE_4:<sum of all EVENT_TYPE_4 for WED}
{EVENT_DATE:THU,EVENT_TYPE_4:<sum of all EVENT_TYPE_4 for THU}
{EVENT_DATE:FRI,EVENT_TYPE_4:<sum of all EVENT_TYPE_4 for FRI}
{EVENT_DATE:SAT,EVENT_TYPE_4:<sum of all EVENT_TYPE_4 for SAT}
{EVENT_DATE:SUN,EVENT_TYPE_5:<sum of all EVENT_TYPE_5 for SUN}
{EVENT_DATE:MON,EVENT_TYPE_5:<sum of all EVENT_TYPE_5 for MON}
{EVENT_DATE:TUE,EVENT_TYPE_5:<sum of all EVENT_TYPE_5 for TUE}
{EVENT_DATE:WED,EVENT_TYPE_5:<sum of all EVENT_TYPE_5 for WED}
{EVENT_DATE:THU,EVENT_TYPE_5:<sum of all EVENT_TYPE_5 for THU}
{EVENT_DATE:FRI,EVENT_TYPE_5:<sum of all EVENT_TYPE_5 for FRI}
{EVENT_DATE:SAT,EVENT_TYPE_5:<sum of all EVENT_TYPE_5 for SAT}
{EVENT_DATE:SUN,EVENT_TYPE_6:<sum of all EVENT_TYPE_6 for SUN}
{EVENT_DATE:MON,EVENT_TYPE_6:<sum of all EVENT_TYPE_6 for MON}
{EVENT_DATE:TUE,EVENT_TYPE_6:<sum of all EVENT_TYPE_6 for TUE}
{EVENT_DATE:WED,EVENT_TYPE_6:<sum of all EVENT_TYPE_6 for WED}
{EVENT_DATE:THU,EVENT_TYPE_6:<sum of all EVENT_TYPE_6 for THU}
{EVENT_DATE:FRI,EVENT_TYPE_6:<sum of all EVENT_TYPE_6 for FRI}
{EVENT_DATE:SAT,EVENT_TYPE_6:<sum of all EVENT_TYPE_6 for SAT}
{EVENT_DATE:SUN,EVENT_TYPE_7:<sum of all EVENT_TYPE_7 for SUN}
{EVENT_DATE:MON,EVENT_TYPE_7:<sum of all EVENT_TYPE_7 for MON}
{EVENT_DATE:TUE,EVENT_TYPE_7:<sum of all EVENT_TYPE_7 for TUE}
{EVENT_DATE:WED,EVENT_TYPE_7:<sum of all EVENT_TYPE_7 for WED}
{EVENT_DATE:THU,EVENT_TYPE_7:<sum of all EVENT_TYPE_7 for THU}
{EVENT_DATE:FRI,EVENT_TYPE_7:<sum of all EVENT_TYPE_7 for FRI}
{EVENT_DATE:SAT,EVENT_TYPE_7:<sum of all EVENT_TYPE_7 for SAT}
{EVENT_DATE:SUN,EVENT_TYPE_8:<sum of all EVENT_TYPE_8 for SUN}
{EVENT_DATE:MON,EVENT_TYPE_8:<sum of all EVENT_TYPE_8 for MON}
{EVENT_DATE:TUE,EVENT_TYPE_8:<sum of all EVENT_TYPE_8 for TUE}
{EVENT_DATE:WED,EVENT_TYPE_8:<sum of all EVENT_TYPE_8 for WED}
{EVENT_DATE:THU,EVENT_TYPE_8:<sum of all EVENT_TYPE_8 for THU}
{EVENT_DATE:FRI,EVENT_TYPE_8:<sum of all EVENT_TYPE_8 for FRI}
{EVENT_DATE:SAT,EVENT_TYPE_8:<sum of all EVENT_TYPE_8 for SAT}
]
Here is what I've tried so far -
function aggregateData(jsonData, dateField) {
console.log(jsonData);
// Days of week
var dayOfWeek = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
// Result array
var resultArray = [];
// Counters
var i, j, k, z;
// Scan through the days of the week
for (i = 0; i < dayOfWeek.length; i++) {
// Loop through the dataprovider
for (k = 0; k < jsonData.length; k++) {
var curr_keyset = _.keys(jsonData[k]);
var curr_valset = _.values(jsonData[k]);
// Loop through current item of the data set
for (z = 0; z < curr_keyset.length; z++) {
// Day of week of current date
var dow = dateFormat(new Date(curr_valset[curr_keyset.indexOf(dateField)]), "ddd").toUpperCase();
resultArray[dow][curr_keyset[z]] += vals[k];
}
}
}
}
aggregateData(jsonData, 'EVENT_DATE'); //passing JSON data & the field name that is the DATE field
This obviously has some mistakes. Could I please request help to correct this. I'm trying too while I ask post this question. If I manage to get it to work, will report back.
CURRENT CODE
var accumulation = {};
var resultArr = [];
jsonData.forEach(function (daily) {
// var dow = dows[dateFormat(new Date(daily[dateField]), "ddd").toUpperCase()];
var dow = dateFormat(new Date(daily[dateField]), "ddd").toUpperCase();
for (var p in daily) {
if (p.indexOf(dateField) === -1) {
accumulation[p] = accumulation[p] | {}; // init if necessary
accumulation[p][dow] = daily[p][dow] | 0; // init if necessary
var valueToAdd = daily[p];
accumulation[p][dow] += valueToAdd;
}
}
resultArr[i] = accumulation;
});
console.log(accumulation);
Upvotes: 0
Views: 69
Reputation: 1204
NOTE - i'm not sure how "jsonData" specifically relates to "array of objects" - i'll assume jsonData isn't a String and is in fact an array of Objects at this point, and i'll skip the underscore stuff since it's not bringing much to the table here (perhaps groupBy() and reduce() could be worked in...)
you probably want to reorder things a bit. First, setup an object you can accumulate things into:
// example data
var jsonData = [
{EVENT_DATE:"SUN", EVENT_TYPE_1:3, EVENT_TYPE_2:0, EVENT_TYPE_3:2, EVENT_TYPE_4:6, EVENT_TYPE_5:0, EVENT_TYPE_6:0, EVENT_TYPE_7:0, EVENT_TYPE_8:26},
{EVENT_DATE:"MON", EVENT_TYPE_1:3, EVENT_TYPE_2:0, EVENT_TYPE_3:2, EVENT_TYPE_4:6, EVENT_TYPE_5:0, EVENT_TYPE_6:0, EVENT_TYPE_7:0, EVENT_TYPE_8:26},
{EVENT_DATE:"TUES", EVENT_TYPE_1:3, EVENT_TYPE_2:0, EVENT_TYPE_3:2, EVENT_TYPE_4:6, EVENT_TYPE_5:0, EVENT_TYPE_6:0, EVENT_TYPE_7:0, EVENT_TYPE_8:26},
{EVENT_DATE:"WED", EVENT_TYPE_1:3, EVENT_TYPE_2:0, EVENT_TYPE_3:2, EVENT_TYPE_4:6, EVENT_TYPE_5:0, EVENT_TYPE_6:0, EVENT_TYPE_7:0, EVENT_TYPE_8:26},
{EVENT_DATE:"SUN", EVENT_TYPE_1:3, EVENT_TYPE_2:0, EVENT_TYPE_3:2, EVENT_TYPE_4:6, EVENT_TYPE_5:0, EVENT_TYPE_6:0, EVENT_TYPE_7:0, EVENT_TYPE_8:26}
];
// accumulate counts
var accumulation = {};
jsonData.forEach(function(daily) {
var dow = daily.EVENT_DATE;
for (var p in daily) {
if (p.startsWith('EVENT_TYPE')) {
accumulation[p] = accumulation[p] || {}; // init if necessary
accumulation[p][dow] = accumulation[p][dow] || 0; // init if necessary
accumulation[p][dow] += daily[p]
}
}
})
console.log(accumulation);
// flatten to results
var flat = [];
for (var et in accumulation) {
for (var dow in accumulation[et]) {
var fi = {EVENT_DATE : dow};
fi[et] = accumulation[et][dow];
flat.push(fi)
}
}
console.log(flat)
so now you have a hash of eventTypes -> daysOfWeek -> count...flattening the hash to get the desired format should be trivial from here.
Upvotes: 1