+ 8
How to declare an empty multidimensional array in JavaScript?
My JavaScript code needs looping to declare a multidimensional array. Is there a best way to replace it? My magic square: https://code.sololearn.com/WBwWcTsdjxcd/?ref=app
11 Réponses
+ 6
Javascript doesn't need formal declaration like C# or other strongly typed languages. Multidimensional arrays are just nested arrays.
My first piece of advice is to do yourself a favor and use descriptive variable names. That code is really hard to read, and while you're in it, it might make sense; six months from now you'll go back and just be lost.
I've created a code to hopefully better explain multidimensional arrays in javascript. There's also an example pattern you can experiment with in the JS panel.
https://code.sololearn.com/Wn75Hc7jTOZw#js
+ 3
up vote
0
down vote
A nested 3x3 array (of undefined):
var arr = new Array(3);
for (var i = 0; i < arr.length; ++i) {
arr[i] = new Array(3);
}
console.log(arr);
+ 2
Reminder, this is a JavaScript discussion, not Java.
+ 1
let width=5;
let height=5;
let emptyArray = [...Array(height)].map(e => Array(width).fill(0));
alert(emptyArray );
- 1
Maulik, I don't think it works like that in javascript 😅
- 1
how do u code this
like what should I get in place first
- 1
To declare a Two Dimensional Array in Java, you can use either
data_type[][] array_name = new data_type[SIZE][SIZE] ;
(or)
data_type array_name[][] = new data_type[SIZE][SIZE] ;
- 2
TO DECLARE TWO DIMANTION ARRAY IN JAVA
I.E TWO DIMANTION ARRAY
DATA TYPE[SIZE][SIZE]=new DATA TYPE
- 2
i told in java not java script..
- 2
it doesn't happen in JavaScript
- 3
do you know how big the multidimensional array will be?