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: javascript define multidimensional array
var iMax = 20;
var jMax = 10;
var f = new Array();
for (i=0;i<iMax;i++) {
f[i]=new Array();
for (j=0;j<jMax;j++) {
f[i][j]=0;
}
}
Example 3: js initialize 2d array
let data = [];
for (let row=0; row<rows; row++) {
data.push(new Array(cols).fill('#'));
};
Example 4: multi-dimensional array js
var array = [
["0, 0", "1, 0", "2, 0", "3, 0", "4, 0"],
["0, 1", "1, 1", "2, 1", "3, 1", "4, 1"],
["0, 2", "1, 2", "2, 2", "3, 2", "4, 2"],
["0, 3", "1, 3", "2, 3", "3, 3", "4, 3"],
["0, 4", "1, 4", "2, 4", "3, 4", "4, 4"],
];
console.log(array[3][3]);
Example 5: 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);
Example 6: javascript multidimensional array
var items = [
[1, 2],
[3, 4],
[5, 6]
];
console.log(items[0][0]);
console.log(items[0][1]);
console.log(items[1][0]);
console.log(items[1][1]);
console.log(items);