gpt4 book ai didi

javascript - 为报告准备自定义数组

转载 作者:行者123 更新时间:2023-12-01 00:14:01 24 4
gpt4 key购买 nike

我想为收款报告准备一个数组我有一个响应数组,例如:

orderDate:[{ 
date: 21-01-20,
sales: 20,
method: Razorpay,
sub: 1
},{
date: 21-01-20,
sales: 20,
method: Simpl,
sub: 1
},{
date: 21-01-20,
sales: 20,
method: Razorpay,
sub: 1
},{
date: 21-01-20,
sales: 20,
method: Cash,
sub: 1
},{
date: 21-01-20,
sales: 20,
method: Credit,
sub: 1
},{
date: 22-01-20,
sales: 10,
method: Credit,
sub: 1
},{
date: 22-01-20,
sales: 20,
method: Razorpay,
sub: 1
}];

我有付款方式数组

paymentMethods:[Razorpay, Simpl, Paytm, Other];

我想准备一个最终数组,例如:

final:[{
date: 21-01-20,
revenue:[40, 20, 0, 40] //method wise sum of sales for the respected date
},{
date: 22-01-20,
revenue:[20, 0, 0, 10] //method wise sum of sales for the respected date
}]

如果没有找到相关方法的数据,则在该方法的索引处推送 0 值

感谢您的帮助

最佳答案

尝试下面的代码:

var orderDate = [{
date: "21 - 01 - 20",
sales: 20,
method: "Razorpay",
sub: 1
}, {
date: "21 - 01 - 20",
sales: 20,
method: "Simpl",
sub: 1
}, {
date: "21 - 01 - 20",
sales: 20,
method: "Razorpay",
sub: 1
}, {
date: "21 - 01 - 20",
sales: 20,
method: "Cash",
sub: 1
}, {
date: "21 - 01 - 20",
sales: 20,
method: "Credit",
sub: 1
}, {
date: "22 - 01 - 20",
sales: 10,
method: "Credit",
sub: 1
}, {
date: "22 - 01 - 20",
sales: 20,
method: "Razorpay",
sub: 1
}];

var paymentMethods = ["Razorpay", "Simpl", "Paytm", "Other"];

var final = [];

orderDate.forEach(dateItem => {
var logItem = final.find(x => x.date === dateItem.date);

if (!logItem) {
logItem = {
date: dateItem.date,
revenue: [0, 0, 0, 0]
}
final.push(logItem);
}


var methodIndex = paymentMethods.indexOf(dateItem.method);

if (methodIndex === -1) {
methodIndex = paymentMethods.indexOf("Other")
}

logItem.revenue[methodIndex] = logItem.revenue[methodIndex] + dateItem.sales
});

console.log(final);

关于javascript - 为报告准备自定义数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59891983/

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