i have a question
i have a question

Reputation: 79

2 arrays changing instead of 1

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]

Upvotes: 5

Views: 158

Answers (6)

varun sharma
varun sharma

Reputation: 1057

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] 
*/

Upvotes: 2

StackSlave
StackSlave

Reputation: 10627

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);

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074198

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;
}

Upvotes: 6

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28455

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}] 

Upvotes: 1

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

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);

Upvotes: 6

Mureinik
Mureinik

Reputation: 311188

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);

Upvotes: 0

Related Questions