5

I am making a game with 2 arrays but one array changes when I don't want it to. Example from the console in browser:

A=[1,2,3,4,5]
B=[6,7,8,9,10]
A=B
A.push(11)
A =[6, 7, 8, 9, 10, 11]
B =[6, 7, 8, 9, 10, 11]

the A is fine but is there a way to make the B stay [6,7,8,9,10]

Eddie
  • 26,593
  • 6
  • 36
  • 58
  • 1
    `A=B` doesn't create a copy. It causes `A` to be a reference to the same memory location; meaning when you change `A`, `B` changes, too, because it's the same object. –  Jun 12 '18 at 06:21
  • If it's not a Primitive value then assignment makes the var exactly the non-Primitive var. https://developer.mozilla.org/en-US/docs/Glossary/Primitive – StackSlave Jun 12 '18 at 06:24
  • A doesn't own `[1,2,3,4,5]`, it only points to `[1,2,3,4,5]` and so `A=B` makes B points to same array by sharing same reference – Isaac Jun 12 '18 at 06:25
  • If you take a `.slice()` of the Array you want to copy, but not affect the original, that usually does the trick. – StackSlave Jun 12 '18 at 06:26

6 Answers6

6

Use spread syntax as A=[...B]; to copy B to A. As when you do A=B you are actually setting the reference of B to A so any changes to A result in changes in B and vice-versa.

var A=[1,2,3,4,5];
var B=[6,7,8,9,10];
A=[...B];
A.push(11);
console.log(A);
console.log(B);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
6

When you do

A=B

you're making A refer to the same array that B points to. So naturally you see any change you make to that array regardless of which variable you use to look at the array they both refer to.

the A is fine but is there a way to make the B stay [6,7,8,9,10]

It sounds like you want to copy the contents of B to A. To do that, you can use slice:

A = B.slice();

Alternately, in modern environments, you can use ES2015's spread notation as Ankit pointed out.

There's no need to assign anything to A initially, since you never use it.

Example:

var A;
var B = [6,7,8,9,10];
A = B.slice();
A.push(11)
console.log(A); // [6, 7, 8, 9, 10, 11]
console.log(B); // [6, 7, 8, 9, 10]
.as-console-wrapper {
  max-height: 100% !important;
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2
A=[1,2,3,4,5]
B=[6,7,8,9,10]

Here are three methods to copy an array into other

 A = JSON.parse(JSON.stringify(B));   

or

A = [].concat(B);

or

A= [...B];       //for ES6

A.push(11);    



/* 
A =[6, 7, 8, 9, 10, 11]
B =[6, 7, 8, 9, 10] 
*/
varun sharma
  • 1,017
  • 15
  • 19
1

By A=B, you are assigning the variable A the same reference of B which means any change in either of the variable will be reflected in the other too as they both share same reference.

You can use Spread Operator in order to create a copy of the array

let A = [1,2,3,4,5], B = [6,7,8,9,10];
A=[...B];
A.push(11);
console.log(A); // [6,7,8,9,10, 11]
console.log(B); // [6,7,8,9,10]

Catch

Please note, if the values are objects, then the objects will still continue to share the same reference.

let A = [{x: 1}], B = [{y:2}];
A = [...B];
A.push({z:3});
A[0].y = 3;
console.log(A); // [{y:3}, {z:3}]
console.log(B); // [{y:3}] 
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
1

Read my comments above. Then:

var a = [1, 2, 3, 4, 5], b = [6, 7, 8, 9, 10];
a = b.slice(); a.push(11);
console.log('a', a); console.log('b', b);
StackSlave
  • 10,613
  • 2
  • 18
  • 35
0

By assigning A=B, both variables point to the same array, and thus modifying it through one will be visible via the other. If you want A and B to refer to two different arrays, you could copy it instead of just assigning. E.g.:

B = A.slice(0);
Mureinik
  • 297,002
  • 52
  • 306
  • 350