使用javascript计算数组的季度销售额和平均值

时间:2016-05-25 13:36:41

标签: javascript

需要以对象格式计算季度销售额和平均值。



var saleItem =  [{"month":"January","date":"1","transactionId":"1","salePrice":"100"},{"month":"February","date":"1","transactionId":"2","salePrice":"200"},{"month":"March","date":"1","transactionId":"3","salePrice":"300"},{"month":"April","date":"1","transactionId":"4","salePrice":"300"},{"month":"May","date":"1","transactionId":"4","salePrice":"300"},{"month":"June","date":"1","transactionId":"4","salePrice":"300"},{"month":"July","date":"1","transactionId":"4","salePrice":"300"},{"month":"August","date":"1","transactionId":"4","salePrice":"300"},{"month":"September","date":"1","transactionId":"4","salePrice":"300"},{"month":"October","date":"1","transactionId":"4","salePrice":"300"},{"month":"November","date":"1","transactionId":"4","salePrice":"300"},{"month":"December","date":"1","transactionId":"4","salePrice":"300"}]
   
 function getSumQuarterly(saleItems) {
//should output 
  /*
  {quarter: 1, totalPrices: xx, transactionNums: 1},
  {quarter: 2, totalPrices: xx, transactionNums: 1},
  {quarter: 3, totalPrices: xx, transactionNums: 1},
  {quarter: 4, totalPrices: xx, transactionNums: 1},
  */
}
function getAverageQuarterly(saleItems) {

}




1 个答案:

答案 0 :(得分:1)

这是请求的代码

var saleItem =  [{"month":"January","date":"1","transactionId":"1","salePrice":"100"},{"month":"February","date":"1","transactionId":"2","salePrice":"200"},{"month":"March","date":"1","transactionId":"3","salePrice":"300"},{"month":"April","date":"1","transactionId":"4","salePrice":"300"},{"month":"May","date":"1","transactionId":"4","salePrice":"300"},{"month":"June","date":"1","transactionId":"4","salePrice":"300"},{"month":"July","date":"1","transactionId":"4","salePrice":"300"},{"month":"August","date":"1","transactionId":"4","salePrice":"300"},{"month":"September","date":"1","transactionId":"4","salePrice":"300"},{"month":"October","date":"1","transactionId":"4","salePrice":"300"},{"month":"November","date":"1","transactionId":"4","salePrice":"300"},{"month":"December","date":"1","transactionId":"4","salePrice":"300"}]

    function getSumQuarterly (saleItems) {
        var sum = [{
            totalPrices: 0,
            quarter: 1,
            transactionNums: 1
        }];
        var thisQuarterIndex = 0;
        saleItems.forEach(function (item, index) {
            var quarterIndex = Math.floor(index/3);
            if (quarterIndex === thisQuarterIndex) {
                sum[quarterIndex]['totalPrices'] = sum[quarterIndex]['totalPrices'] + parseInt(item.salePrice, 10);
            } else {
                thisQuarterIndex = quarterIndex;
                sum.push({});
                sum[quarterIndex]['totalPrices'] = parseInt(item.salePrice, 10);
                sum[quarterIndex]['quarter'] = quarterIndex + 1;
                sum[quarterIndex]['transactionNums'] = 1;
            }
        });
        return sum;
    }
    function getAverageQuarterly(saleItems) {
        var sum = getSumQuarterly(saleItems);
        var avg = [];
        sum.forEach(function (quarter) {
            avg.push(quarter.totalPrices/3);
        });
        return avg;
    }