Я выполняю вычисления в массиве вложенных объектов в 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
}]