javascript 2d array for loop code example
Example 1: nested array loop in javascript
let chunked = [[1,2,3], [4,5,6], [7,8,9]];
for(let i = 0; i < chunked.length; i++) {
for(let j = 0; j < chunked[i].length; j++) {
console.log(chunked[i][j]);
}
}
Example 2: javascript two dimensional array
var grid = [];
iMax = 3;
jMax = 2;
count = 0;
for (let i = 0; i < iMax; i++) {
grid[i] = [];
for (let j = 0; j < jMax; j++) {
grid[i][j] = count;
count++;
}
}
// grid = [
// [ 0, 1 ]
// [ 2, 3 ]
// [ 4, 5 ]
// ];
console.log(grid[0][2]); // 4