javascript two dimensional array problem

+1 Sirius Black · November 12, 2015
var array = [0,0,0];
var array2d = [
  array,
  array,
  array
];

array2d[0][0] = 5;
array2d[1][0] = 10;
array2d[2][0] = 40;

console.log(array2d[0][0]);

the output shows that the value at array2d [0] [0] is 40.
so  if i modify any array at any row of this two dimensional array, it changes every other array. can anyone explain why?

my logic suggests that the value of array2d[0][0] should remain at 5
because, in my sense line 2 is equivalent to :
var array2d = [
[0,0,0],
[0,0,0],
[0,0,0]
];

but its clearly not equivalent. 

is this because of circular array object? if so please explain how it works.
thanks in advance.

Post a Reply

Replies

Oldest  Newest  Rating
0 Sirius Black · November 12, 2015
yes....that's the answer i was looking for...i wasn't sure about it, i just had to be sure.


Thank you so much!!!!! 


:D
  • 1

Javascript

107,354 followers
About

A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.

Links
Moderators