x


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.

more ▼

asked Feb 22, 2010 at 06:14 PM

Ashkan_gc gravatar image

Ashkan_gc
10k 76 91 160

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

This question has been moot since Unity 3.2. In fact you can do

var a : float[,];

with no problem.

more ▼

answered Nov 30, 2011 at 08:09 AM

Eric5h5 gravatar image

Eric5h5
107k 52 180 648

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Feb 22, 2010 at 06:46 PM

Eric5h5 gravatar image

Eric5h5
107k 52 180 648

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

Feb 23, 2010 at 05:00 AM Ashkan_gc

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....

Feb 23, 2010 at 06:20 AM Eric5h5

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.

Feb 23, 2010 at 07:41 AM Ashkan_gc

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.

Feb 23, 2010 at 08:23 AM Eric5h5

really!? i don't think so. I'll ask it from support team.

Feb 26, 2010 at 01:41 PM Ashkan_gc
(comments are locked)
10|3000 characters needed characters left

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;
}
more ▼

answered Feb 23, 2010 at 07:47 AM

Ashkan_gc gravatar image

Ashkan_gc
10k 76 91 160

(comments are locked)
10|3000 characters needed characters left

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 :)

more ▼

answered Nov 30, 2011 at 07:49 AM

matthew 2 gravatar image

matthew 2
0 21 14 17

The OP kind of specifically said that they didn't want this.

Nov 30, 2011 at 07:50 AM syclamoth

Also, you can just use multidimensional arrays. You really don't want to use the Array class.

Nov 30, 2011 at 08:10 AM Eric5h5
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5884
x687

asked: Feb 22, 2010 at 06:14 PM

Seen: 28296 times

Last Updated: 2 days ago