Sometimes, we want to create multidimensional array with JavaScript.
In this article, we’ll look at how to create multidimensional array with JavaScript.
How to create multidimensional array with JavaScript?
To create multidimensional array with JavaScript, we can use a for loop.
For instance, we write
let row = 20;
let column = 10;
let f = new Array();
for (i = 0; i < row; i++) {
f[i] = new Array();
for (j = 0; j < column; j++) {
f[i][j] = 0;
}
}
to create a nested for loop that adds the nested arrays to the f
array with the outer loop.
In the inner loop, we fill the nested arrays with 0’s with
for (j = 0; j < column; j++) {
f[i][j] = 0;
}
Conclusion
To create multidimensional array with JavaScript, we can use a for loop.