declaring two dimensional array in javascript code example
Example 1: js array two dimensional
const matrix = new Array(5).fill(0).map(() => new Array(4).fill(0));
console.log(matrix[0][0]);
Example 2: multi dimensional array javascript
let activities = [
['Work', 9],
['Eat', 1],
['Commute', 2],
['Play Game', 1],
['Sleep', 7]
];
Example 3: 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);