Say I have arrays inside an array:
time = 0
arr = [[1,2,3], [4,5,6], [7,8,9]]
And I try to log the arr in each while loop
while (time<3){
currentarr = JSON.parse(JSON.stringify(arr))
for(i=0;i<currentarr.length;i++){
for(j=0; j<currentarr[i].length; j++){
if(currentarr[i][j]>2){
arr[i][j]++
}
}
}
console.log(arr)
time++
}
My problem is that each while loop gave me the same arr (the end result after three loops):
[[1, 2, 6],[7, 8, 9],[10, 11, 12]]
instead of for each time:
[[1, 2, 4],[5, 6, 7],[8, 9, 10]]
[[1, 2, 5],[6, 7, 8],[9, 10, 11]]
[[1, 2, 6],[7, 8, 9],[10, 11, 12]]
I just begin learning Javascript and I can't figure out which part went wrong... Thanks in advance!
let (item of arr)pattern, personally it saves my brain a lot of time compared tofor (var i = 0; i < arr.length; i++) { arr[i] ... }, which is a lot less readable.