简体   繁体   English

根据季度计算总和

[英]Calculating sum based on quarters

I am trying to get the sum of totals based on quarter system. 我试图获得基于四分之一系统的总计。 [10,20,30,40,50,60,70,80,90] - Initial array [10,20,30,40,50,60,70,80,90] -初始数组

The result I am looking for is [60, 150, 70, 80, 90] Where Q1 = 10+20+30 = 60 Q2 = 40+50+60 = 150 Q3 will be complete only if pass in October value. 我要寻找的结果是[60, 150, 70, 80, 90]其中Q1 = 10+20+30 = 60 Q2 = 40+50+60 = 150 Q3仅在十月份通过时才是完整的。

I am confused with the logic I have 我对自己的逻辑感到困惑

 var adata = [10,20,30,40,50,60,70,80,90]; process(adata, "Sept 2014"); function process(adata, date){ var today = new Date(date) || new Date(); var aQuarter = []; var tempQuarter = []; var aSum = []; //var quarter = Math.floor((today.getMonth() + 3)/3); for(var i = 0; i<= today.getMonth(); i++) { aQuarter.push(adata[i]); } tempQuarter = adata.slice(); if(tempQuarter.length > 3) { var i = 0; var sum = []; while(i <= 2) { sum[i] = aQuarter.shift(); i++; } console.log(sum); aQuarter.unshift(sum); } if(tempQuarter.length > 6){ var i = 0; var sum = []; while(i+1 <= 3) { sum[i] = aQuarter.shift(); i++; } aQuarter.unshift(sum); } console.log(aQuarter); if(tempQuarter.length > 9){ var i = 0; while(i+2 <= 4) { sum[i] = aQuarter.shift(); i++; } } } 

jsfiddle jsfiddle

Sorry played around with the fiddle a bit and got this to get your results, will it work for you? 抱歉,小提琴演奏了一下,得到了这个结果,对您有用吗?

var adata = [10,20,30,40,50,60,70,80,90];
console.log("Sept 2014");
process(adata, "Sept 2014");

console.log("April 2014");
process(adata, "April 2014");

console.log("March 2014");
process(adata, "March 2014");

console.log("December 2014");
process(adata, "December 2014");

function process(adata, date){
    var today = new Date(date) || new Date();

    // reults array init
    var tempArray = [];
    var eachQuarterAmount = 3;
    // Limit the values to January to today as defined by the date param
    var aDataUpToPoint = adata.slice(0,today.getMonth() + 1);
    console.log(aDataUpToPoint);

    for (var i = 0; i < aDataUpToPoint.length; i+=eachQuarterAmount) {
    // increment every 3 values

        if (typeof aDataUpToPoint[i+eachQuarterAmount] != 'undefined') {
            // If there's anything after (ie if there's Oct) then total previous values up using reduce
            tempArray.push(aDataUpToPoint.slice(i,i+eachQuarterAmount).reduce(function(total, num){ return total + num },0));
        } else {
            // If the hunt for red October failed just tack on whatever remains
            for (; i < aDataUpToPoint.length; i++) {
                // uses the same loop contol variable as the parent to complete the search in both cases, picks up where it left off
                tempArray.push(aDataUpToPoint[i]);
            }
        }
    }
    console.log(tempArray);
}

Edit: reduce some magic numbers... sorta. 编辑:减少一些魔术数字...排序。

Note: Just realized you were doing something with a date param... so really my adata.length should probably be replaced with today.getMonth() 注意:刚刚意识到您正在使用日期参数进行操作...因此,实际上我的adata.length应该应该替换为today.getMonth()

Edit 2: sliced aData down to work within a date range, added a +1 to the data set and removed the +1 from the look ahead. 编辑2:将aData切片以在日期范围内工作,向数据集添加+1,并从前瞻中删除+1。

I have a bit shorter version with loop to sum quarters value only. 我有一个较短的版本,带有循环以仅求和四分之一的值。 Unused elements are concatenated at the end. 最后,将未使用的元素串联起来。

var adata = [10,20,30,40,50,60,70,80,90];
process(adata, "Sept 2014");

function process(adata, date) {
  var today = new Date(date) || new Date();
  var QUOTER = 3;
  var month = today.getMonth();
  var limit = (month > adata.length) ? adata.length : month;
  var aQuarter = []; // quater sum array
  var sum = 0;       // gather sum for each quarter
  for (var i=0; i < limit; i++) {
    sum += parseInt(adata[i]);
    if ((i+1) % QUOTER == 0) {
      aQuarter.push(sum);
      sum = 0;
    }
  }
  var usedElements = aQuarter.length * QUOTER;
  if (adata.length > usedElements)
    aQuarter = aQuarter.concat(adata.slice(usedElements));

  console.log("On " + today + " the sums are " + aQuarter);
}

Here is DEMO that you can play with date setting. 这是您可以使用日期设置播放的演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM