I have come across an interesting issue with my Javascript code. I am working with two objects, dict1 and dict2, where I need to maintain a form of state and then post the final object.
When I click on certain buttons, I assign dict1 to dict2. However, when I try to delete a key in dict2, it also removes the key from dict1, which is not what I intended. Below is my code snippet:
dict1 = {
123:{
456:2
}
}
dict2 = {}
dict2[123] = dict1[123]
delete dict2[123][456]
console.log(dict1)
// It logs {123 : {} } whereas dict1 should remain unchanged
It appears that the objects are being shallow copied somehow.