3

When the same object reference exists in two arrays, the objects are equivalent, and updating one affects the other.

However deleting the object from one array does not delete it in the other.

Why not?

var a1 = [
  {i: 0, s: 'zero'}, 
  {i: 1, s: 'one'}
];

var a2 = [
  a1[0],
  a1[1]
];

// items point to same reference
print(a1[0] === a2[0]); // true (equivalent)

// updating one affects both
a1[0].s += ' updated';
print(a1[0] === a2[0]); // true (still equivalent)
print(a1[0]); // {"i":0,"s":"zero updated"}
print(a2[0]); // {"i":0,"s":"zero updated"}

// however, deleting one does not affect the other
delete a1[0];
print(a1[0]); // undefined
print(a2[0]); // {"i": 0, "s": "zero"}

Interestingly, deleting a property from one, does affect the other.

delete a1[1].s;
print(a1[1]); // {"i":1}
print(a2[1]); // {"i":1}

https://jsfiddle.net/kevincollins/4j6hj2v7/3/

6
  • 13
    You delete a reference to the value, not the value itself. See it like assigning a name to a person. If you stop calling them Chris they don't suddenly disappear. Commented May 31, 2017 at 12:44
  • 2
    Objects are assigned using reference. So when you do a1[0], it will fetch its reference and copy it in array and will use it to access value. So when you delete reference, you remove the original location and hence it will reflect in all variables Commented May 31, 2017 at 12:46
  • Don't use delete on arrays. Just set the index to null or undefined. Commented May 31, 2017 at 12:47
  • @Bergi Since arrays are objects, how is using delete harmful? I get that you're creating a holed array, but that isn't a problem to standard APIs Commented May 31, 2017 at 12:48
  • @MayorMonty Yes, thats true, but you are replacing the value stored in array and not changing the ref itself Commented May 31, 2017 at 12:51

2 Answers 2

1

To answer why last print(a2[0]); still shows value, lets start analyzing the code.

Your a1 is an array and when you initialize it with objects, it will create objects and store their reference.

var a1 = [
  {i: 0, s: 'zero'}, // ref 1001
  {i: 1, s: 'one'}   // ref 1002
];

This part, by your comments is clear, but what happens when you do delete a1[0]?

Will it remove the object? Answer is No. It will remove the property stored at 0th index in a1 and set it to undefined. But if you delete the property of the object held at that reference, it will show in both: sample

What happens to the object then? The value is retained and will be garbage collected if no one is referring it. In your case, since a2[0] still is accessing it, it will retain the value.

You can check following sample for reference.

Sign up to request clarification or add additional context in comments.

Comments

0

Like @Halcyon said, you are deleting the reference, not the object itself. To demonstrate, look at the following example:

var a = 2
var b = [a, 2, 3, 4]

If you were to delete b[0], you would only delete the reference, and thus, a===2. However, if you were to delete the reference at a, via delete a, then the value in b would be changed to undefined, because the reference that used to exist at that location no longer does

2 Comments

That's kinda what I was expecting. However in this example, a doesn't get deleted. jsfiddle.net/kevincollins/f30euchx
@KevinCollins It's important to remember that literals (numbers, strings, etc.) cannot be deleted and if you reference one, you cannot delete it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.