0

I want to create an Object that contains one or more two dimensional arrays in Javascript.

I tried it the following way (in this example I only try to add one two dimensional array):

var XSIZE = 8;  
var YSIZE = 8;  
var obj = {  
 field : new Array(XSIZE),  
 field[0] : new Array(YSIZE),  
 foo : 1,  
 bar : 100  
}

Info:
- This gives me a strange error "missing : after property id" which does not seem to make much sense
- Unfortunately I didn't find examples showing how to do this so far by using google
- If I don't add field[0] ... for creating the 2nd array it works.
- changing the XSIZE and YSIZE to numbers like new Array(8)... doesn't work.

I would really appreciate if somebody could show me how to do it or explain why I cannot do this at all and need to use some other method.

Thanks a lot!

Timo Sperisen
  • 463
  • 7
  • 8
  • Possible duplicate of... http://stackoverflow.com/questions/966225/how-to-create-a-two-dimensional-array-in-javascript – danmux Mar 10 '12 at 19:34

4 Answers4

3

The error "missing : after property id" is because JavaScript sees the field part of field[0] and expects a colon before the value of that field. Instead it gets an open bracket so it complains.

You can't hard code an object definition that has its dimensions set up at run time. You have to build the object at run time as well. Like this perhaps

var XSIZE = 8;  
var YSIZE = 8;

var obj = {  
 field : new Array(),  
 foo : 1,  
 bar : 100  
}

for (var i = 0; i < XSIZE; i++) {
  obj.field.push(new Array(YSIZE));
}
Borodin
  • 126,100
  • 9
  • 70
  • 144
1

In object literal notation, the property names must be exactly that: property names. Firstly, field[0] isn't a property name. Secondly, the properties don't exist until the after the object defined, so you can't access properties until then.

What you should do is either set the array after the object is created:

var obj = {...}
obj.field[0] = [...];

or nest the array literals:

var obj = {
    field: [ [...],
              ...
           ],
    ...
}

You don't need to worry about setting the array size when creating the array, as it will grow when you add elements.

outis
  • 75,655
  • 22
  • 151
  • 221
  • Hi @outis. Didn't see your response when I tried Borodin's response but it seems to go in the same direction as his response. I'll also give you +1 as soon as I can. Thanks! – Timo Sperisen Mar 10 '12 at 19:40
  • 1
    @outis: but you *do* need to set the size of all but the last dimension of multi-dimensional arrays. That is the main problem pim has here. You can't just write `var field = [[]]; field[7][7] = 99`, so all his `XSIZE` second-level arrays have to be created and pushed into the first-level array. – Borodin Mar 10 '12 at 19:44
  • @Borodin: the problem with your example isn't that the size of the arrays aren't pre-declared, it's that the entries aren't pre-declared. `var field = []; field[7] = []; field[7][7] = 99;` works fine. In no way does something like `Array(8)` help in your example. – outis Mar 10 '12 at 19:57
0

You could get the two dimensional array as your obj property, without resorting to external procedures and keep everything internal to the object. Create your empty 'field' array 1st.

var obj = {
field:[],

foo:1,
bar:100
};

Now, create an object's method to create a two dimensional array off your initial dimensionless array. You can determine the length and the number of dimensions of multi dimension array as you wish at run time:

var obj = {
field:[],
multifield:function(x,y){for (var count=0;count<x;count++)  {this.field[count]=new Array(y);}}, 

foo:1,
bar:100
};

You can then call the obj.multifield method entering whatever dimensions you decide:

obj.multifield(10,5); //-->create a 10x5 array in this case...

console.log(obj.field.length); // 10
 console.log(obj.field[0].length); // 5
0

You can only declare properties on the object being constructed that way; not on objects in another "level".

You could use a for loop instead:

for(var i = 0; i < XSIZE; i++) {
  obj.field[i] = new Array(YSIZE);
}

Note that the YSIZE is not necessary since an empty array works just fine as well ([]).

pimvdb
  • 151,816
  • 78
  • 307
  • 352