我试图根据季度系统得到总数的总和。
data= [{
"rowLabel": "Label",
"previousYearSpend": "21239481.8",
"janActual": "13146105.51",
"janPlan": "13126631.26",
"febActual": "129458614.21",
"febPlan": "12440428.3",
"marActual": "15269255.37",
"marPlan": "1497070.55",
"aprActual": "16996329.67",
"aprPlan": "15683061.15",
"mayActual": "13736166.96",
"mayPlan": "14847570.78",
"junActual": "135525666.19",
"junPlan": "14849827.86",
"julActual": "",
"julPlan": "15908197.25",
"augActual": "",
"augPlan": "",
"sepActual": "",
"sepPlan": "",
"octActual": "",
"octPlan": "",
"novActual": "",
"novPlan": "",
"decActual": "",
"decPlan": ""
}]
我要找的结果是
"marActual": "15269255.37",
"marPlan": "1497070.55",
"aprActual": "16996329.67",
"aprPlan": "15683061.15",
"mayActual": "13736166.96",
"mayPlan": "14847570.78",
"junActual": "135525666.19",
"junPlan": "14849827.86",
"julActual": "",
"julPlan": "15908197.25"
其中marActual = janActual+febActual+marActual
marPlan = janPlan+febPlan+marPlan
我将日期作为
'2015/JUN/01'
,结果应为Q1, APR, MAY, JUN
如果我将日期作为'2015/JUL/1'
传递,则应为Q1, Q2, JUL
其中junActual = aprActual+mayActual+junActual
和junPlan = aprPlan+mayPlan+junPlan
我对我必须对值fiddle求和的逻辑感到困惑。 我的数据表是基于这些数据构建的,它动态创建表并使用此json呈现表。
答案 0 :(得分:0)
这是一个解决方案,但它有点笨重:/我会看看我是否可以清理它。
data= [{
"rowLabel": "Label",
"previousYearSpend": "21239481.8",
"janActual": "13146105.51",
"janPlan": "13126631.26",
"febActual": "129458614.21",
"febPlan": "12440428.3",
"marActual": "15269255.37",
"marPlan": "1497070.55",
"aprActual": "16996329.67",
"aprPlan": "15683061.15",
"mayActual": "13736166.96",
"mayPlan": "14847570.78",
"junActual": "135525666.19",
"junPlan": "14849827.86",
"julActual": "",
"julPlan": "15908197.25",
"augActual": "",
"augPlan": "",
"sepActual": "",
"sepPlan": "",
"octActual": "",
"octPlan": "",
"novActual": "",
"novPlan": "",
"decActual": "",
"decPlan": ""
}];
quarterActual = function(){
monthsCorrect = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']; //store months in correct order
Q1 = monthsCorrect.slice(0,3);
Q2 = monthsCorrect.slice(3,6);
Q3 = monthsCorrect.slice(6,9);
Q4 = monthsCorrect.slice(9,12);
totaQ1 = 0;
totaQ2 = 0;
totaQ3 = 0;
totaQ4 = 0;
for (var k = 0; k < Q1.length; k++) {
a1 = Q1[k] + 'Actual';
a2 = Q2[k] + 'Actual';
a3 = Q3[k] + 'Actual';
a4 = Q4[k] + 'Actual';
totaQ1 += Number(data[0][a1]);
totaQ2 += Number(data[0][a2]);
totaQ3 += Number(data[0][a3]);
totaQ4 += Number(data[0][a4]);
}
return([String(totaQ1), String(totaQ2), String(totaQ3), String(totaQ4)]);
}
quarterPlan = function() {
monthsCorrect = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']; //store months in correct order
Q1 = monthsCorrect.slice(0,3);
Q2 = monthsCorrect.slice(3,6);
Q3 = monthsCorrect.slice(6,9);
Q4 = monthsCorrect.slice(9,12);
totpQ1 = 0;
totpQ2 = 0;
totpQ3 = 0;
totpQ4 = 0;
for (var k = 0; k < Q1.length; k++) {
p1 = Q1[k] + 'Plan';
p2 = Q2[k] + 'Plan';
p3 = Q3[k] + 'Plan';
p4 = Q4[k] + 'Plan';
totpQ1 += Number(data[0][p1]);
totpQ2 += Number(data[0][p2]);
totpQ3 += Number(data[0][p3]);
totpQ4 += Number(data[0][p4]);
}
return([String(totpQ1), String(totpQ2), String(totpQ3), String(totpQ4)]);
}
quarterlyReport = function(date){ // date can be in your format, '2015/JUN/01', or even just the abbreviation
date = date.toLowerCase();
monthsCorrect = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']; //store months in correct order
quarterReport = {}; // the quarter report
// find which month to stop at using regular expression
for (i = 0; i < monthsCorrect.length; i++){
var monRegEx = RegExp(monthsCorrect[i]);
var test = monRegEx.test(date);
if (test == true){break;}
}
var monthStop = i; //index that for loop above stopped at
included = monthsCorrect.slice(0,monthStop + 1); // months to report back
if(included.length == 3){
quarterReport.Q1Actual = quarterActual()[0];
quarterReport.Q1Plan = quarterPlan()[0];
} else if (included.length > 3 && included.length <= 6){
quarterReport.Q1Actual = quarterActual()[0];
quarterReport.Q1Plan = quarterPlan()[0];
// report months after Q1
monthsAfter = monthsCorrect.slice(Q1.length, included.length)
for (m = 0; m < monthsAfter.length; m++){
act = monthsAfter[m] + 'Actual';
pl = monthsAfter[m] + 'Plan';
quarterReport[act] = data[0][act];
quarterReport[pl] = data[0][pl];
}
} else if (included.length > 6 && included.length <= 9){
quarterReport.Q1Actual = quarterActual()[0];
quarterReport.Q1Plan = quarterPlan()[0];
quarterReport.Q2Actual = quarterActual()[1];
quarterReport.Q2Plan = quarterPlan()[1];
monthsAfter = monthsCorrect.slice(Q1.length*2, included.length)
for (n = 0; n < monthsAfter.length; n++){
act = monthsAfter[n] + 'Actual';
pl = monthsAfter[n] + 'Plan';
quarterReport[act] = data[0][act];
quarterReport[pl] = data[0][pl];
}
} else if (included.length > 9 && included.length <= 12){
quarterReport.Q1Actual = quarterActual()[0];
quarterReport.Q1Plan = quarterPlan()[0];
quarterReport.Q2Actual = quarterActual()[1];
quarterReport.Q2Plan = quarterPlan()[1];
quarterReport.Q3Actual = quarterActual()[2];
quarterReport.Q3Plan = quarterPlan()[2];
monthsAfter = monthsCorrect.slice(Q1.length*3, included.length)
for (n = 0; n < monthsAfter.length; n++){
act = monthsAfter[n] + 'Actual';
pl = monthsAfter[n] + 'Plan';
quarterReport[act] = data[0][act];
quarterReport[pl] = data[0][pl];
}
} else {
for(var j = 0; j < included.length; j++){
actual = included[j] + 'Actual';
plan = included[j] + 'Plan';
quarterReport[actual] = data[0][actual];
quarterReport[plan] = data[0][plan];
}
}
return(quarterReport);
}
console.log(quarterlyReport('2015/JUN/01'));
6月份,这将返回:
{Q1Actual: "157873975.09"
Q1Plan:"27064130.110000003"
aprActual:"16996329.67"
aprPlan:"15683061.15"
junActual:"135525666.19"
junPlan:"14849827.86"
mayActual: "13736166.96"
mayPlan: "14847570.78"}
顺便说一句,我运行了你的脚本,如果March应该返回累计总数,那么我认为Q1actual
(marActual
)的数量是错误的。我加起来是1月,2月和3月,结果是我在Q1Actual上的总数。
答案 1 :(得分:0)
这是我提出的解决方案,如果有比我更好的解决方案,我将使用该代码fiddle
var aMonths = ["jan", "feb", "mar",
"apr", "may", "jun",
"jul", "aug", "sep",
"oct", "nov", "dec"],
data= [{
"rowLabel": "Label",
"previousYearSpend": "21239481.8",
"janActual": "13146105.51",
"janPlan": "13126631.26",
"febActual": "12958614.21",
"febPlan": "12440428.3",
"marActual": "15269255.37",
"marPlan": "1497070.55",
"aprActual": "16996329.67",
"aprPlan": "15683061.15",
"mayActual": "13736166.96",
"mayPlan": "14847570.78",
"junActual": "135525666.19",
"junPlan": "14849827.86",
"julActual": "",
"julPlan": "15908197.25",
"augActual": "",
"augPlan": "",
"sepActual": "",
"sepPlan": "",
"octActual": "",
"octPlan": "",
"novActual": "",
"novPlan": "",
"decActual": "",
"decPlan": ""
}];
console.log(sumByQuarter('2015/APR/01', data, aMonths));
function sumByQuarter(date, data, aMonths) {
//console.log(data);
var today = new Date(date) || new Date(),
QUARTER = 3,
month = today.getMonth(),
limit = (month > aMonths.length) ? aMonths.length : month,
aData = [],
tempMonths = aMonths.slice(),
sumActual = 0;
sumPlan = 0;
tempMonths.splice(limit + 1, Number.MAX_VALUE);
var aQuarter = []; // quater array
var sumjson = {};
for(i = 0; i < limit; i++) {
sumActual += parseFloat(data[0][tempMonths[i]+'Actual']);
sumPlan += parseFloat(data[0][tempMonths[i]+'Plan']);
if((i+1) % QUARTER == 0 ) {
sumjson[tempMonths[i]+'Actual'] = sumActual;
sumjson[tempMonths[i]+'Plan'] = sumPlan;
aQuarter.push(tempMonths[i]);
sumActual = 0;
sumPlan = 0;
}
}
console.log(sumjson);
var usedElements = aQuarter.length * QUARTER;
if(tempMonths.length > usedElements) {
aQuarter = aQuarter.concat(tempMonths.slice(usedElements));
}
//console.log(aQuarter);
for(j = 0; j < data.length; j++) {
var json = {};
for(i = 0; i < aQuarter.length; i++) {
json['rowLabel'] = data[j]['rowLabel'];
json['previousYearSpend'] = data[j]['previousYearSpend'];
json[aQuarter[i]+'Actual'] = typeof sumjson[aQuarter[i] + 'Actual'] != 'undefined' ? sumjson[aQuarter[i] + 'Actual'] : data[j][aQuarter[i] + 'Actual'];
json[aQuarter[i]+'Plan'] = typeof sumjson[aQuarter[i] + 'Plan'] != 'undefined' ? sumjson[aQuarter[i] + 'Plan'] : data[j][aQuarter[i] + 'Plan'];
}
aData.push(json);
}
//console.log(aData);
return aData;
}