Javascript - 数据聚合/求和值

时间:2016-08-05 03:42:41

标签: javascript jquery underscore.js

我有一个对象数组,如下所示

[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]

数组中有几百个对象。在下面扩展其中一些 -

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"

我想将每个EVENT_TYPE的值加到一周的几天中。生成如下输出 -

[
{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}          
]

这是我到目前为止所尝试的内容 -

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

这显然有一些错误。我可以请求帮助来纠正这个问题。我也在尝试发布这个问题。如果我设法让它工作,将报告回来。

当前代码

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);

1 个答案:

答案 0 :(得分:1)

注意 - 我不确定&#34; jsonData&#34;特别涉及&#34;对象数组&#34; - 我假设jsonData不是字符串,实际上是此时的一个对象数组,我会跳过下划线的东西,因为它没有带来很多东西(也许groupBy()和reduce()可以在...中工作

你可能想重新订购一下。首先,设置一个可以积累东西的对象:

   // 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)

所以现在你有一个eventTypes的哈希 - &gt; daysOfWeek - &gt; count ...展平哈希以获得所需的格式应该是微不足道的。