[英]javascript sum array values in multidimensional array with conditions
I'm trying to use javascript to iterate over an object and sum the values of a property grouping by the value of another property. 我正在尝试使用javascript迭代一个对象,并通过另一个属性的值对属性分组的值求和。 Here is my example data that I am iterating over: 这是我正在迭代的示例数据:
63.450, 2013-01-01 00:00:00, 63.450, 2013, Amex Residuals
3.980, 2013-01-01 00:00:00, 3.980, 2013, Gift Cards on Demand
-16.000, 2013-01-01 00:00:00, -16.000, 2013, Month End Fee Rejects
67.140, 2013-02-01 00:00:00, 67.140, 2013, Amex Residuals
-600.000, 2013-02-01 00:00:00, -600.000, 2013, Bonus Take Back - Closed Less Than 6 Months
-400.000, 2013-02-01 00:00:00, -400.000, 2013, Bonus Take Back - Did Not Activate
8.910, 2013-02-01 00:00:00, 8.910, 2013, Checks On Demand
13997.770, 2013-02-01 00:00:00, 13997.770, 2013, Global Report
-15.000, 2013-02-01 00:00:00, -15.000, 2013, Merchant Adjustments
-34.500, 2013-02-01 00:00:00, -34.500, 2013, Month End Fee Rejects
The data continues to include other months (through october) in the second column. 数据继续包括第二列中的其他月份(至10月)。 I need to sum all the values in the first column together for each distinct month, as well as create a javascript date from the 4th column year, so the result should be something like this: 我需要将每个不同月份的第一列中的所有值加起来,并从第4列年份创建一个javascript日期,因此结果应该是这样的:
var data = [ [Date.UTC('2013', i, 1), parseFloat('51.43')], [Date.UTC('2013', i, 1), parseFloat(13024.32)] ];
Essentially I should end up with a 2 element array for each month total tupled with a date object from the 4th column. 基本上我应该最终得到一个2元素数组,每个月总共使用第4列的日期对象。 I'm just not sure how to do the iterations for summing on the conditional grouping of the 2nd column (date). 我只是不确定如何对第二列(日期)的条件分组进行求和的迭代。
The general process is: 一般过程是:
Without knowing your exact structures, here's some semi-pseudo-code 在不知道确切结构的情况下,这里有一些半伪代码
var data = {}
for (var i = 0; i < lines.length; ++i) {
var monthYear = lines[i][1].substring(0, 6);
var value = 0;
if (data[monthYear]) {
value = data[monthYear];
}
value += +lines[i][0]; // unary + to make sure the value is a number, not a string
data[monthYear] = value;
}
(Obviously, if you have objects instead of arrays, you can access them appropriately.) (显然,如果你有对象而不是数组,你可以适当地访问它们。)
Based on @scottmermelstein's answer (which I will accept because it led me to this) I created the following, which gives me the results I'm looking for: 根据@ scottmermelstein的答案(我会接受,因为它引导我这个)我创建了以下内容,它给了我正在寻找的结果:
var objCalc = {'unCalc':{}, 'reCalc':{}},
objUnCalc = {},
objReCalc = {},
arrUnCalc = [],
arrReCalc = [];
//lets sum up the month totals first, and create the date object that is needed for the graph
$.each(data.TableContainer, function(i,e){
objCalc.unCalc[ e.ActualDate ] = ( objCalc.unCalc[ e.ActualDate ] ? objCalc.unCalc[ e.ActualDate ] + parseFloat(e.GrpTotal) : parseFloat(e.GrpTotal) );
objCalc.reCalc[ e.ActualDate ] = ( objCalc.reCalc[ e.ActualDate ] ? objCalc.reCalc[ e.ActualDate ] + parseFloat(e.GrpBonusAmt) : parseFloat(e.GrpBonusAmt) );
});
//now we iterate over the summed values from above and push them to usable highcharts arrays
$.each(objCalc, function(i,e){
$.each(e, function(y,el){
var arrDate = y.substring(0,y.indexOf(' ')).split('-'), //renders something like ['2013','02','01']
UTC = Date.UTC(arrDate[0], parseInt(arrDate[1])-1, parseInt(arrDate[2])), //subtract 1 from month to make UTC date 0-based
arr = [ UTC, parseFloat( parseFloat( el ).toFixed( 2 ) ) ];
if( i == "unCalc" ) arrUnCalc.push( arr );
if( i == "reCalc" ) arrReCalc.push( arr );
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.