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?
Think of it like this:
If you change the key for one of them (say the latter) to a new object you’ll just get this:
(Or the key will simply be missing if you delete it:)
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:
(Or delete both keys:)
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.
The a) option seems to make the most sense given your use case. If you have both keys anyway, why not update/delete both?