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.

ghchoi
  • 4,812
  • 4
  • 30
  • 53
  • 1
    they share the reference, try copying the array and sort, it's not a problem with the sort(). – Tinu Jos K Feb 11 '20 at 13:43
  • Does this answer your question? [Copy array by value](https://stackoverflow.com/questions/7486085/copy-array-by-value) – TIGER Feb 14 '20 at 03: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... – ghchoi Feb 14 '20 at 03:12

4 Answers4

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();
Michael Bianconi
  • 5,072
  • 1
  • 10
  • 25
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.

bryan60
  • 28,215
  • 4
  • 48
  • 65
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

Tinu Jos K
  • 888
  • 1
  • 8
  • 21
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)
Sam Anderson
  • 301
  • 2
  • 8