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: n dimensional array js
function createArray(length) {
var arr = new Array(length || 0),
i = length;
if (arguments.length > 1) {
var args = Array.prototype.slice.call(arguments, 1);
while(i--) arr[length-1 - i] = createArray.apply(this, args);
}
return arr;
}
createArray();
createArray(2);
createArray(3, 2);
Example 3: how to read 2 dimensional array in javascript
activities.forEach((activity) => {
activity.forEach((data) => {
console.log(data);
});
});
Example 4: creating a 2d array in js
var x = new Array(10);
for (var i = 0; i < x.length; i++) {
x[i] = new Array(3);
}
console.log(x);
Example 5: 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 6: javascript fill 2 dimensional array
const fillSquareMatrix = (size) => {
return Array(size)
.fill()
.map((u,y) => Array(size)
.fill()
.map((u,x) => y * size + x + 1));
};
console.log(fillSquareMatrix(3));