So I just discovered today that doing this:
a = { b: { c: 1, d: 2 }, d: {} }
sub = a.b
// sub is { c: 1, d: 2 }
sub is now actually the object stored in a, not a clone.
Now if I do this:
sub.c = "x"
// a is now: { b: { c: 'x', d: 2 }, d: {} } // nice
The same thing applies to arrays.
So I have this array:
arr = [{a: 1, b: 2}, {c: 3, d: 4}]
sub = arr[1]
I'd like to remove sub from array so that arr becomes: [{a: 1, b: 2}] but if I do sub = null I simply assign a new value to sub. Same thing for delete.
delete sub // will unset the sub variable, not the object that it references.
So the question is: how do I remove {c: 3, d: 4} from the array by using sub
Even though it works, I can't use delete arr[1] because I don't know the index. I'm storing the object using the min function of lodash
arr[1]andsubare both references to the same object, butsubis not pointer to the array itself.sub.c = "x"andsub = null. In the first case you are reading the value ofsuband mutate the object, in the second you are writing a new value tosub.