I have found myself in a peculiar problem with Javascript. I am trying to maintain a form of state object/ dictionary (dict1) and a final object/ dictionary(dict2) which I have to post.
I assign the object dict1 to my object dict2 when clicking on some buttons. What I want to do is delete the key in dict2 but the behavior is peculiar as it also deletes the key from the dict1. Below is my code:
dict1 = {
123:{
456:2
}
}
dict2 = {}
dict2[123] = dict1[123]
delete dict2[123][456]
console.log(dict1)
// It logs {123 : {} } whereas the dict1 shouldn't change
It seems like the objects are shallow copying somehow.