- Home /
how to create multidimensional arrays in javascript
we can create arrays in javascript with a notation like
var a : float[];
but i can not create multidimensional arrays in javascript like
var a : float[,];
how can i create a two dimensional array in javascript? i want .NET arrays and don't want array class arrays.
Answer by Eric5h5 · Nov 30, 2011 at 08:09 AM
This question has been moot since Unity 3.2. In fact you can do
var a : float[,];
with no problem.
Well, as of Unity 2017.1.1f1, BCE0077: It is not possible to invoke an expression of type 'float[,]'. so... Try again.
UnityScript is a dead language. It's no longer maintained, it's no longer officially supported and will be completely removed in future Unity versions so you shouldn't really bother. Though this still works fine:
// UnityScript
var a : float[,];
function Start () {
a = new float[3,2];
}
Thanks, that works great. Looks like I might have been doing it wrong. I had honestly tried searching the Unity $$anonymous$$anual and left some nice (as possible) feedback to the Unity $$anonymous$$m about how difficult it is to find the most basic things... And about how it takes 10 seconds to run a single search... Thus why I turned to googling 8 year old forum posts... >_>
Yes, you do need to try again. The Unityscript compiler hasn't changed in years; the answer also has not changed, and will continue to be correct until they remove Unityscript entirely. You do realize that this question isn't about C#.
I'm coding in JavaScript. That's the whole reason I came to a question with the title "How to ... in javascript". What more evidence can I give that I'm coding in JavaScript?
Answer by Eric5h5 · Feb 22, 2010 at 06:46 PM
While you can use multi-dimensional arrays in Javascript, the syntax for creating them is missing. Fortunately you can get around that by using type inference, as shown in this helper script.
thank you eric! i don't use js myself but i want to help a friend to do this in javascript. i searched in mono's website and many other pages but nothing found. thank you again
Yeah, it's a Unity limitation. It should be possible to directly declare arrays the way you wrote, without using type inference work-arounds, but currently it's not. Hopefully it will be fixed sometime....
mono's JS compiler is not an active part of the development. they might support js by DLR in the future but there is no plans about that too. take a look at this. if unity upgrade it's mono version then we can add support for scripting in our applications with DLR. mono supports DLR and we can easily make our games scriptable with IronPython or rubby. they'll upgrade it soon.
That's not really relevant. Unity's Javascript is unique to Unity, and is developed by the creator of Boo, Rodrigo Barreto de Oliveira, and uses a lot of code from the Boo implementation.
really!? i don't think so. I'll ask it from support $$anonymous$$m.
Answer by Ashkan_gc · Feb 23, 2010 at 07:47 AM
thank you guys for the answers. so unity's javascript compiler or mjs (mono jscript compiler) is not capable of declaring multiDimensional arrays but can use them. they are first class members of the .NET framework and are not language dependent so you can declare them in C# and return them and use them in JS. in fact when you declare an array in C# it will crate an instance of System.Array or one of it's children. the problem is js don't support this syntax for rectangular arrays but surely it can use the class. you should declare a static method for that for easier use.
static int[,] intArray(int d1,int d2)
{
return new int[d1,d2];
}
you can define a function for each type of use reflection to create a generic method. generics themselves are not supported in js so you can not simply write
static t[,] arr<t>(int d1,int d2)
{
return new t[d1,d2];
}
i mean you can write but in js you can not write
var t = arr<int>(10,30);
you can make a generic method using typeof operator and system.type class and ... remember if your array is an array of a reference type (a class) then you should populate all elements before returning it.
public static string[,] createString (int x,int y)
{
string s[,] = new string[x,y]; //this will declare an array of string in size x X y but all elements are null.
//you should populate them yourself by hand.
for (int i=0;i<x;i++)
for (int j=0;j<y;j++)
s[i,j]= "new string"; // for other types of classes you might use new classname();
//then you can return s
return s;
}
Answer by matthew 2 · Nov 30, 2011 at 07:49 AM
You can just use this:
var YourArrayHere = new Array();
YourArrayHere.length = [first dimension array length here];
for(var count = 0; count < YourArrayHere.length; count++)
{
var TempSecondArray = new Array();
TempSecondArray.length = [sec dimension array length here];
YourArrayHere[count] = TempSecondArray;
}
And it can be accesed with
YourArrayHere[First dimension index][Second dimension index]
It may be long.. but this is the way i do it :)
The OP kind of specifically said that they didn't want this.
Also, you can just use multidimensional arrays. You really don't want to use the Array class.