- Home /
How do I make an array of arrays in javascript
I'm just learning and I wanted to do something simple so I figured making an array grid in javascript would be easy enough. However, no matter what I do I keep getting the error ArguementOutOfRangeException. Any advice of information on what I did wrong would be greatly appreciated.
P.S I'm sure that there's a more efficient way of doing this, like with lists or something, but I'm trying to practice with arrays.
#pragma strict
public var iMax = 10;
public var jMax = 10;
public var f : [];
public var i: int;
public var j : int;
function Start ()
{
for (i=0;i < iMax;i++)
{
f[i]= [];
for (j=0; j < jMax; j++)
{
Debug.Log (f[i][j]);
}
}
}
I have this script attached to a random object, I figured it would work on anything but maybe I'm wrong.
thanks, but I'm more curious about what I did wrong. A friend of $$anonymous$$e said my code should work in theory. Thanks for the link though.
Answer by Yinoguns · Aug 18, 2014 at 09:52 PM
While i'm not all that Familiar with JavaScript (I prefer C# myself) I'm not sure about your code in general.
Such as why you would need an Array, of arrays, why not just a multidimensional array.
I found this link that shows literal declaration of an array, but is somewhat different to your code. ( var arlene = [ ]; ) http://freewebdesigntutorials.com/javaScriptTutorials/jsArrayObject/declareAnArray.htm
If I were to work off your code, I would hazard the guess that you are creating object f of type array so:
public var f : [ ]
Then, for X times (10) you make each part of F into an Array.
f[ [ ] ] or f[i] = [ ]
In which case I would say the whole thing then should work, other than theres no data other than empty arrays so im not sure how Unity would feel about that.
How about you make an Array Var, which contains Arrays of ints, perhaps then something might print?
Just some ideas and thoughts.