基于月份和年份的聚合数组

时间:2016-03-25 12:16:00

标签: javascript jquery mapreduce

我有一个像这样的数组

var data = [[1411151400000,1686],[1428604200000,1686],[1411151400000,1686]....]

我需要根据月份对这些数据进行分组,并总结该月的数据。

最终输出应该是这样的。

 var final = [[timestamp for month, sum of value of that month],[]]

下面是我试过的代码,但它没有给出正确的结果,我在想是否可以通过Map减少数组来完成

if(aggregatorType === "month")
{

        sum = 0;

            $.each(datalooper,function(key,value){
                var date =  new Date(value[0]);
                var month = date.toString("MMMM");
                var year = date.toString("yyyy");
                var temparr = [];
                var lastdate = 0;
                if(!iterator[month+"_"+year]){
                    iterator[month+"_"+year] = value;
                    temparr.push(value[0]);
                    temparr.push(sum);
                    if(sum != 0)
                        newArr.push(temparr);
                    sum = 0;
                }
                else{
                    sum = sum+ value[1];
                    lastdate=value[0];
                }
            })
                totalObj.push(newArr);
            newArr = [];
            iterator = {};

    }
    return totalObj;
}

3 个答案:

答案 0 :(得分:0)

  

var final = [[月份的时间戳,当月的价值总和],[]]

先找几个月

var newData = [];
var months = {};
data.forEach(function(value){
   var date = new Date(value[0]);
   date.setDate(1);
   date.setHours(0,0,0,0);
   var month = date.getTime();
   if( !months[ month ] )
   {
      months[ month ] = 0;
   }
   months[ month ] += value[1];
}); 

现在把它变成一个数组

newData = Object.keys( months ).map( function(key){ return [ key, months[key] ]; } );

这将以您要查找的格式为您提供输出。

答案 1 :(得分:0)

可能更容易创建一个对象然后映射回您想要的结果对象,这里有按月和年映射的对象与您拥有的堆栈

var mappings = datalooper.map(function(v) {
        var nDate = new Date(v[0]);
        return {
            year: nDate.getYear(),
            month: nDate.getMonth(),
            data: v[1]
        };
    }).reduce(function(result, curr) {

        if (result[curr.year] === undefined) {
            result[curr.year] = {}
        }

        if (result[curr.year][curr.month] === undefined) {
            result[curr.year][curr.month] =[];
        }

        result[curr.year][curr.month].push(data

        return result;
});
/*if you want to put them in an array do the following
but not sure how you will keep track of which month is which */
return Object.keys(mappings)
      .map(function(v){ 

           return Object.keys(mappings[v])
                        .map(function(v){ return v ; } );
      });

答案 2 :(得分:0)

获得年份和月份并将值分组的解决方案。

Date#toString没有要设置的参数。

使用Date#getYearDate#getMonth检索年份和月份。

var data = [[1411151400000, 1686], [1428604200000, 1686], [1411151400000, 1686]],
    grouped = function (array) {
        var r = [], o = {};
        array.forEach(function (a) {
            var date = new Date(a[0]),
                month = date.getMonth() + 1,
                key = date.getFullYear() + '-' + (month < 10 ? '0' : '') + month;
            if (!o[key]) {
                o[key] = [key, 0];
                r.push(o[key]);
            }
            o[key][1] += a[1];
        });
        return r;
    }(data);

document.write('<pre>' + JSON.stringify(grouped, 0, 4) + '</pre>');

相关问题