I have always used a 'for' loop to calculate sums in tables, but recently I learned about a better way - using the 'reduce' method. Following documentation and examples with simple arrays was easy enough, but now I am working with an array of objects. Here is my code, and I know there must be a simple mistake that I'm overlooking:
let dataRevenus = [
{ id: 1, label: intl.get("REVENUES_FONCIERS"), value: 230000000 },
{ id: 2, label: intl.get("REVENUES_VALUERS_MOBILIERES"), value: 25000000 },
{ id: 3, label: intl.get("PENSIONS_RETRAITES"), value: 33008.0 }
];
let test = 0;
let sum = dataRevenus
? dataRevenus.reduce((acc, item) => {
console.log("here : " + parseFloat(item.value));
test = test + parseFloat(item.value);
return test;
})
: 0;
console.log(sum);
My console output shows that the first item is not being included in the calculation. This is the result I get:
here : 25000000
here : 33008
25033008
Despite getting the correct total sum, it seems like I'm missing out on including the value of the first item
Any assistance or advice would be greatly appreciated