javascript - objects immutable?
javascript
Solution
Think of it like this:
data[key1][key2] -----> object1
^
data[key2][key1] ---------+
If you change the key for one of them (say the latter) to a new object you'll just get this:
data[key1][key2] -----> object1
data[key2][key1] -----------> object2
(Or the key will simply be missing if you delete it:)
data[key1][key2] -----> object1
data[key2]
That is, you'll change one of the references but not the other. Any attempt to replace only one of them will accomplish this. You have two options:
a) Change both keys to point to the new object:
data[key1][key2] -----> object2
^
data[key2][key1] ---------+
(Or delete both keys:)
data[key1][key2] object1 (no references, will be garbage collected)
data[key2][key1]
b) Modify a field on the object itself. This way they'll both still be pointing to the same object, but something about it will be different. Any code that you use will have to take this into account, though.
data[key1][key2] -----> object1 with 'isDeleted'=true
^
data[key2][key1] ---------+
The a) option seems to make the most sense given your use case. If you have both keys anyway, why not update/delete both?
Problem
I'm attempting to create something of a tree structure. I wish to access my data as follows: ``` data[key1][key2] ``` However, key1 and key2 have a symmetric relationship; the following is always true: ``` data[key1][key2] == data[key2][key1] ``` More specifically, I have `data[key1][key2]` and `data[key2][key1]` pointing to the same object, such that changes to one affect the other. My problem arises because I wish to delete the underlying object. I know if I use: ``` delete data[key1][key2]; ``` `data[key2][key1]` still refers to the object. My question is this: is there any way to remove the underlying object, or overwrite it with something falsey, such that both properties above will evaluate falsey?