我正在 JavaScript 中执行嵌套对象数组的计算。 我有两个输入,如下所示:
对于obj
,如果 obj.rate > mr
,则:
cr = (((obj.amount * obj.rate) - (obj.amount * mr))/(obj.amount * obj.rate))*100 + "%",
totalcost = (obj.netfee-(cr*amountwithfee)
如果 obj.rate < mr
,则:
cr = (((obj.amount * mr) - (obj.amount * obj.rate))/(obj.amount * mr))*100 + "%",
totalcost = (obj.netfee+(cr*amountwithfee)
如何在下面的函数中精确地执行上述计算:
var result = obj.forEach(e=>{
..obj,
netfee: obj.fee + obj.payfee,
amountwithfee: obj.amount-obj.netfee,
cr: (((obj.amount * mr) - (obj.amount * obj.rate))/(obj.amount * mr))*100 + "%",
totalcost: (obj.netfee+(cr*amountwithfee);
})
console.log(result);
输入
var mr = 0.5;
var obj =[{
amount: 1000,
fee: 5,
payfee:2,
rate:0.49
}]
预期输出:
result = [{
amount: 1000,
fee: 5,
payfee: 2,
netfee: 7,
amountwithfee: 993,
rate: 0.49,
cr : -2%,
totalcost: 26.86
}]
obj.forEach
但是obj
不是一个数组。此外,forEach()
没有返回任何内容。你是从一个对象还是一个对象数组开始的? - Mark