I am encountering a weird problem with what I believe to be the way Javascript references variables. The project I am working in is Typescript, but this functionality is from vanilla Javascript.
I have an object all_obj set further up in the code.
The following code:
all = [];
data = [];
console.log(all_obj);
Outputs:
{
"1593561600000": {
"date": 1593561600000,
"volume": 24463,
"value": 165049285,
"rank": 0
},
"1596240000000": {
"date": 1596240000000,
"volume": 24366,
"value": 158841976,
"rank": 0
},
"1604188800000": {
"date": 1604188800000,
"volume": 30034,
"value": 196655815,
"rank": 0
},
This goes on 9 more times with similar objects
}
This, works as expected.
However the following code somehow logs something completely different:
let all = [];
let data = [];
console.log(all_obj);
for (let key of Object.keys(all_obj)) {
all.push(all_obj[key]);
}
this.data.push(
{
brand: 'all_aggregated',
datapoints: [...all],
},
{
brand: 'all_mean',
datapoints: [...all].map((dp: brand_datapoint) => {
dp.value = +(dp.value / dp.volume).toFixed(2);
return dp;
}),
}
);
It logs this:
{
"1593561600000": {
"date": 1593561600000,
"volume": 24463,
"value": 6746.89,
"rank": 0
},
"1596240000000": {
"date": 1596240000000,
"volume": 24366,
"value": 6519,
"rank": 0
},
"1604188800000": {
"date": 1604188800000,
"volume": 30034,
"value": 6547.77,
"rank": 0
},
9 more like this
}
As you can see, in the second example. The "value"'s within the all_obj's children is now in the thousands, instead of the hundreds of millions. Also, it is formatted to 2 decimal places. This is suspiciously the formatting I am doing in this line towards the end of the second code example:
dp.value = +(dp.value / dp.volume).toFixed(2);
How on earth is changing the values propagating back up the code???
Especially through [...all]
This is running in an Angular Project. I have trimmed off the bloat, if you need more info of course ask for it, but I didn't want to paste 300 lines of code in here :)
I have been fighting with this for about 6 hours now, and have completely rewritten the same code in about 15 different ways. So I am completely baffled what is causing this.
Solution! The issue isn't that the data is propagating back up to all_obj, it's that values were being set in the pink arrow, and were being picked up in the red arrow because they were just referencing to it. I thought [...all] would fix it but that just builds a new array with the same references.