在下面的代码中是否有更好/简单的方法来计算 servicecost 和 gatewayCost?我正在遍历一个 json 数组以创建另一个 json 对象来更新数据库中的结果。代码工作正常,但我觉得所使用的逻辑不是一个理想的逻辑。任何简化以下代码的帮助将不胜感激。>
var shippingCharge=20;
var serviceChargePercent =5;
var gstPercentOnServiceCharge=5;
var paymentGatewayPercent =2;
var cart=
[
{
"id": 9,
"title": "Unero Military Classical Backpack",
"is_featured": false,
quantity:2,
"is_hot": false,
"price": 42.2,
"sale_price": 35.89,
"vendor": "Young shop",
"review": 3,
"variants": [],
},
{
"id": 18,
"title": "Samsung UHD TV 24inch",
"is_featured": false,
quantity:2,
"is_hot": false,
"price": 599,
"sale_price": null,
"vendor": "Robert's Store",
"review": 4,
"variants": [],
}
]
tableData = cart.map((product, index) => ({
payment_mode: this.state.paymentMode,
invoiceID: parseInt(lastInvoiceId) + 1,
productTitle: product.title,
buyerEmailId: emailID, `enter code here`
servicecost: (
product.quantity *
product.price *
(serviceChargePercent / 100) +
product.quantity *
product.price *
(gstPercentOnServiceCharge / 100)
).toFixed(2),
gatewayCost: (
(parseFloat((product.quantity * product.price).toFixed(2)) +
parseFloat(shippingCharge.toFixed(2)) +
parseFloat(
(
product.quantity *
product.price *
(serviceChargePercent / 100) +
product.quantity *
product.price *
(gstPercentOnServiceCharge / 100)
).toFixed(2)
)) *
(paymentGatewayPercent / 100)
).toFixed(2),
}));