what is 2d array in javascript code example
Example 1: 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 2: how to make a 4 dimensional array in JavaScript
function Array2D(x, y){
let arr = Array(x);
for(let i = 0; i < y; i++){
arr[i] = Array(y);
}
return arr;
}
function Array3D(x, y, z){
let arr = Array2D(x, y);
for(let i = 0; i < y; i++){
for(let j = 0; j < z; j++){
arr[i][j] = Array(z);
}
}
return arr;
}
function Array4D(x, y, z, w){
let arr = Array3D(x, y, z);
for(let i = 0; i < x; i++){
for(let j = 0; j < y; j++){
for(let n = 0; n < z; n++){
arr[i][j][n] = Array(w);
}
}
}
return arr;
}
let myArray = Array4D(10, 10, 10, 10);