I am trying to create an object that has no value on it.
for (let i = 0; i < chartyearsale.length; i++) {
var year = chartyearsale[i].year,
grp = chartyearsale[i].family,
qnt = chartyearsale[i].qnt,
qntsk = chartyearsale[i].qntsk,
fat = chartyearsale[i].total;
total[year] = Object.assign({
[grp]: {
val1: (total[year][grp].val1 || 0) + val1,
val2: (total[year][grp].val2 || 0) + val2,
val3: (total[year][grp].val3 || 0) + val3
}
}, total[year]);
}
The values "year, group, value1, value2 and value3" are all defined.
I am getting this response:
Cannot read properties of undefined (reading 'grp')
I believe this should be done differently:
(total[year][grp].val1 || 0)
//should return 0 if undefined, but it breaks the script!