I have an object named “order”, and I am trying to change some values within in. Below is the code that I use, but it is showing me some weird behaviour.
All console.log output are included as comments after the corresponding line. As far as I can tell, the code is working because when I try to console.log the new value, it is correctly printed on the console.
The problem happens at my last line of code. When I am printing the entire object on console, and navigate to the two values that I just changed, they are both showing the old value “0.60”, instead of the new value “1.00”.
What is causing this bug?
console.log("this.calculate_discount(product)", this.calculate_discount(product)); // 0.40
console.log("order.SubTotalDiscount", this.order.SubTotalDiscount); // 0.60
this.order.SubTotalDiscount = parseFloat(this.calculate_discount(product)) +
parseFloat(this.order.SubTotalDiscount);
this.order.SubTotalDiscount = parseFloat(this.order.SubTotalDiscount).toFixed(2);
console.log("order.SubTotalDiscount", this.order.SubTotalDiscount); // 1.00
console.log("order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount",
this.order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount); // 0.60
this.order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount =
parseFloat(this.calculate_discount(product)) +
parseFloat(this.order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount);
this.order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount =
parseFloat(this.order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount).toFixed(2);
console.log("order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount",
this.order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount); // 1.00
console.log("order", this.order);
Thank you!