js array two dimensional
// declaration of a two-dimensional array
// 5 is the number of rows and 4 is the number of columns.
const matrix = new Array(5).fill(0).map(() => new Array(4).fill(0));
console.log(matrix[0][0]); // 0
js array two dimensional
// declaration of a two-dimensional array
// 5 is the number of rows and 4 is the number of columns.
const matrix = new Array(5).fill(0).map(() => new Array(4).fill(0));
console.log(matrix[0][0]); // 0
how to make a 4 dimensional array in JavaScript
/*having the previous array easy to make is very useful*/
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;
}
/*making the array*/
let myArray = Array4D(10, 10, 10, 10);
n dimensional array js
// function to create a n-dimentional array
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;
}
//eg.
createArray(); // [] or new Array()
createArray(2); // new Array(2)
createArray(3, 2); // [new Array(2),
// new Array(2),
// new Array(2)]
js initialize 2d array
let data = [];
for (let row=0; row<rows; row++) {
data.push(new Array(cols).fill('#'));
};
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;
}
}
javascript two dimensional array
var grid = [];
iMax = 3;
jMax = 2;
count = 0;
for (let i = 0; i < iMax; i++) {
grid[i] = [];
for (let j = 0; j < jMax; j++) {
grid[i][j] = count;
count++;
}
}
// grid = [
// [ 0, 1 ]
// [ 2, 3 ]
// [ 4, 5 ]
// ];
console.log(grid[0][2]); // 4
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us