I am facing a similar object structure with the goal of summing up the same property grouped by year (e.g., 2016 --> importo1: 7500, importo2: 0, importo3: 0, importo4: 3000)
{
index: 0,
annoDelibera: 2020,
importo1: 2500,
importo2: 3000,
importo3: 0,
importo4: 2000
},
{
index: 1,
annoDelibera: 2019,
importo1: 2500,
importo2: 0,
importo3: 0,
importo4: 1000
},
{
index: 2,
annoDelibera: 2016,
importo1: 2500,
importo2: 0,
importo3: 0,
importo4: 1000
},
{
index: 3,
annoDelibera: 2016,
importo1: 2500,
importo2: 0,
importo3: 0,
importo4: 1000
},
{
index: 4,
annoDelibera: 2016,
importo1: 2500,
importo2: 0,
importo3: 0,
importo4: 1000
},
{
index: 5,
annoDelibera: 2015,
importo1: 2500,
importo2: 0,
importo3: 0,
importo4: 1000
},
{
index: 6,
annoDelibera: 2014,
importo1: 2500,
importo2: 0,
importo3: 0,
importo4: 1000
},
{
index: 7,
annoDelibera: 2014,
importo1: 2500,
importo2: 0,
importo3: 0,
importo4: 1000
}
How can I achieve this? My issue is that when I try to sum, it sums all the properties by year and not the individual properties.
getSumGrouped(year) {
let holder = {};
let sum = 0;
for (let entry of this.config.fields) {
for (let row of this.data) {
if (holder.hasOwnProperty(row.annoDelibera) && holder.hasOwnProperty(entry.name)) {
if(year == row.annoDelibera){
sum += Number(row[entry.name]);
holder[row.annoDelibera] = holder[row.annoDelibera] + sum;
}
}
else {
if(year == row.annoDelibera){
sum += Number(row[entry.name]);
holder[row.annoDelibera] = sum;
}
}
}
fields.push({ style: { ...entry.style, ...entry.cellStyle }, value: holder[year] });
}
}
I attempted it in this manner, where this.config.fields provides me with the same name property but from another source. And this.data represents the object structure data mentioned above.