I am new in JavaScript, I would like to initialize a 2D array and change one of the character. However, when I change one of the array value e.g. abc[1,1], the whole column changed. I would like to ask why and how to prevent it? i have tried to use .slice to make a copy, but seem dont work
My prefered result
- - -
- 1 -
- - -
The actual ans:
- 1 -
- 1 -
- 1 -
//My code:
var abc = new Array(3,3)
for(var i =0; i<3;i++)
for(var j =0; j<3;j++)
abc[i,j]="-"
abc[1,1] ="1"
for(var i =0; i<3;i++){
for(var j =0; j<3;j++)
document.writeln(abc[i,j]+" ")
document.writeln("<br \>")
}