我正在開發類似 Food Panda 的應用程式。我正面臨一個問題。我正在計算司機的支出(例如,如果司機每個訂單賺取 4.3 美元)我試圖獲得每個月的總和,但是當司機停止服務時,我的代碼為這些月份分配了相同的價值。
請告訴我我的代碼做錯了什么:
parameters: toDate=2020-10-21&fromDate=2021-11-21
const resturantOrders = await DelivigoOrder.findAll({
attributes: [
"Id",
"OrderNumber",
"OrderStatusId",
"TotalPrice",
"OrderAcceptTime",
"DriverId",
"ResturentId"
],
where: {
DriverId: driverId,
IsActive: true,
// OrderAcceptTime: { [Op.between]: [fromDate, toDate] },
$or: [
{ OrderStatusId: 50 },
{ OrderStatusId: 55 },
{ OrderStatusId: 60 },
{ OrderStatusId: 70 },
{ OrderStatusId: 80 },
],
},
});
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct ", "Nov ", "Dec"];
var orderPayouts = [];
let payout = 0;
for (let index = 0; index < months.length; index ) {
const element = months[index];
for (let index = 0; index < resturantOrders.length; index ) {
const order = resturantOrders[index];
if (element === InternalServices.getMonthName(order.OrderAcceptTime)) {
console.log(element, index, "in");
payout = payout 4.3;
}
}
orderPayouts.push({
month: element,
payout: payout
});
};
輸出
{
"result": [{
"month": "Jan",
"payout": 55.899999999999984
}, {
"month": "Feb",
"payout": 55.899999999999984
}, {
"month": "Mar",
"payout": 55.899999999999984
}, {
"month": "Apr",
"payout": 55.899999999999984
}, {
"month": "May",
"payout": 55.899999999999984
}, {
"month": "Jun",
"payout": 55.899999999999984
}, {
"month": "Jul",
"payout": 55.899999999999984
}, {
"month": "Aug",
"payout": 55.899999999999984
}, {
"month": "Sep",
"payout": 55.899999999999984
}, {
"month": "Oct",
"payout": 150.5
}, {
"month": "Nov",
"payout": 150.5
}, {
"month": "Dec",
"payout": 150.5
}]
}
驅動程式僅在 1 月和 10 月的兩個月內作業,但我的代碼將 Jan 值分配給與 OCT 相同的所有其他月份。
uj5u.com熱心網友回復:
您ley payout在回圈之外宣告。您應該考慮在回圈內宣告它
...
for (let index = 0; index < months.length; index ) {
let payout = 0;
const element = months[index];
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/339489.html
標籤:javascript 节点.js 循环
下一篇:檢索訂閱條帶node.js的狀態
