I wanted to write some code that flattens the values of an object.
But I do not understand why the following code snipped changes the value of the original data !!!
let data = {'row1' : {'a11':10, 'a12':11},
'row2' : {'a21':23, 'a22':11}}
let flat = Object.assign(...Object.values(data))
console.log('flat : ',flat)
console.log('data : ',data)
will result in
flat : { a11: 10, a12: 11, a21: 23, a22: 11 }
data : {
row1: { a11: 10, a12: 11, a21: 23, a22: 11 }, <--- WHY!!!!!!!!!!!
row2: { a21: 23, a22: 11 }
}
This even happenes if I freeze data,
let data = {'row1' : {'a11':10, 'a12':11},
'row2' : {'a21':23, 'a22':11}}
data = Object.freeze(data)
let flat = Object.assign(...Object.values(data))
console.log('flat : ',flat)
console.log('data : ',data)
which also results in
flat : { a11: 10, a12: 11, a21: 23, a22: 11 }
data : {
row1: { a11: 10, a12: 11, a21: 23, a22: 11 }, <--- WHY!!!!!!!!!!!
row2: { a21: 23, a22: 11 }
}
What is going on?