gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:52:19 24 4
gpt4 key购买 nike

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

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) {

}

最佳答案

这是请求的代码

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;
}

关于javascript - 使用 javascript 从数组计算季度销售额和平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37438908/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com