I recall being able to clone an object with the spread operator like such:
let obj1 = {
key: 'value',
key2: 'value2'
}
let obj2 = { ...obj1 }
But now I realize this doesn't work as I am trying to change values of obj2 and its also changing the values in obj1.
If I did:
obj2.key2 = 'test'
It would change obj1.key2 to 'test' as well.
Why is this happening?
I also tried doing:
let obj2 = Object.assign({}, obj1)
but i face the same issues.
Thanks
let obj1 = {
key1: 'value1',
key2: 'value2'
}
let obj2 = { ...obj1 }
obj2.key2 = 'changed key2 value'
console.log( obj1.key2 == obj2.key2 )