1

I'm trying to define an array b to be an independent copy of an array argument a. Thus, I want to change b without changing a.

But here is a mystery: When I sort b, a is sorted as well!

function firstDuplicate(a) {
    let b = a;

    console.log(`this is a : ${a}`)  // this is a : 2,1,3,5,3,2 
    console.log(`this is b : ${b}`)  // this is b : 2,1,3,5,3,2

    b.sort()                    
    console.log(`this is a : ${a}`)  // this is a : 1,2,2,3,3,5 (my problem)
    console.log(`this is b : ${b}`)  // this is b : 1,2,2,3,3,5
}

firstDuplicate([2, 1, 3, 5, 3, 2])

How can I avoid this?

Thank you so much for your replies.

3
  • 1
    they share the reference, try copying the array and sort, it's not a problem with the sort(). Commented Feb 11, 2020 at 13:43
  • Does this answer your question? Copy array by value Commented Feb 14, 2020 at 3:11
  • It happens because of 'shallow copy'. let b = JSON.parse(JSON.stringify(a)); Would do very deep copy. Actually, created a new instance of the same information... Commented Feb 14, 2020 at 3:12

4 Answers 4

4

You aren't duplicating a, you're just creating a new reference to it. Copy the array with slice(), then sort it.

let b = a.slice().sort();
Sign up to request clarification or add additional context in comments.

Comments

0

Arrays are stored by reference in javascript, so when you do

 let b = a

You’re not duplicating, you’re just setting b and a to point at the same array. This would work:

 let b = [...a]

This declares a new array and uses the spread operator to fill it with the contents of a

But be careful because if objects or arrays are inside your original array, the two different arrays are still pointing at the same objects or arrays.

Comments

0

by doing

let a = b;  

you are copying the reference, so any change made to the either of the array will reflect in the other array.

here you have to do a copy,

let a = [...b];   

But using the spread operator to copy the array will create a shallow copy, if you want to do a deep copy of the array.

let a = JSON.parse(JSON.stringify(b));

and then perform sort on it

Comments

0

As mentioned, in Javascript the line let b = a is essentially saying make b point to the same array as a. This is because Arrays are stored by reference. As a result any modifications made to a will also be applied to b.

The solution is to create a new array and let b point to this. This can be done multiple ways

let b = Array.from(a)
let b = [...a]
let b = a.slice().join('')

Each of these returns a new Array rather than a reference to the original.

To prove this you could run the following

let a = [1,2,3];
let b = a;
let c = Array.from(a);

a === b  // true
a === c  // false (because it is a new value)

a[2] = 'new';   // [1,'new',3]

a === b // true (because b also points to [1,'new',3])
a === c // false (c still equals [1,2,3] and is still pointing to a different value)

Comments

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.