i try to initial my array using this
// first approach
createInitial(){
let result = []
let temp = []
console.log(this.grid)
for(let j=0;j<this.grid;j++){
temp.push(false)
}
for(let i=0;i<this.grid;i++){
result.push(temp)
}
when i want to change value ex. result [0][0] = true result will be
result = [[true,false],[true,false]]
Full code https://github.com/son1122/tic-tac-toe
If i use Hard code for initial like this
result = [[false,false],[false,false]]
the result will be correct as i want
result = [[true,false],[false,false]]
i believe that instead to push value it push reference to the same variable and i make it to work now. however i curios why this happen.so i want to ask is
- this normal?
- and what can i do to make
result.push(temp)
work or i should use different approach. or do we have a way to push value instead of ref - can someone provide more information about this ?
thank you everyone for provide any help.