declare array javascript multidimensional code example
Example 1: js initialize 2d array
let data = [];
for (let row=0; row<rows; row++) {
data.push(new Array(cols).fill('#'));
};
Example 2: javascript create 2d array
function create2DArray(rows, columns, value = (x, y) => 0) {
var array = new Array(rows);
for (var i = 0; i < rows; i++) {
array[i] = new Array(columns);
for (var j = 0; j < columns; j++) {
array[i][j] = value(i, j);
}
}
return array;
}
var array = create2DArray(2, 3, (row, column) => row + column);
console.log(array);