【发布时间】:2019-09-26 20:26:02
【问题描述】:
我在 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()也不返回任何东西。你是从一个对象还是对象数组开始? -
@MarkMeyer 感谢您的回复,对不起,它的对象数组。更新代码
标签: javascript jquery arrays object