最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Two Dimensional Array in Javascript Object - Stack Overflow

programmeradmin2浏览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!

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!

Share Improve this question asked Mar 10, 2012 at 19:25 Timo SperisenTimo Sperisen 4838 silver badges8 bronze badges 1
  • Possible duplicate of... stackoverflow./questions/966225/… – danmux Commented Mar 10, 2012 at 19:34
Add a ment  | 

4 Answers 4

Reset to default 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 plains.

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));
}

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.

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 ([]).

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)

  1. 暂无评论