假设我有一个数组数组:
[[2012-12-20, 10], [2012-12-20, 25], [2012-12-19, 10], [2012-12-20, 5]]
如何通过不同的DATE对各个值进行求和来重新组织这个数组,所以我有这样的事情:
[[2012-12-20, 40], [2012-12-19, 10]]
我尝试过这样的事情:
var array = [[2012-12-20, 10], [2012-12-20, 25], [2012-12-19, 10], [2012-12-20, 5]]; pastDate = ''; pastVal = ''; for(var data in array) { var curDate = data[0]; var curVal = data[1]; // ?? // Can't figure out how to organize each value of array organized by distinct date. // Sorry for any fail, I tottaly sux in arrays. }
有人?
提前致谢!
答案 0 :(得分:0)
试试这个:
http://jsbin.com/aNeQArek/2/edit
var g=[['2012-12-20', 10], ['2012-12-20', 25], ['2012-12-19', 10], ['2012-12-20', 5]];
var a={};
for( var i=0;i<g.length;i++)
{
a[g[i][0]] = (a[g[i][0]] || 0) + g[i][1] ;
}
console.log(a);
结果:(它是您想要的值的对象)。
{2012-12-20: 40, 2012-12-19: 10}