Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to define arrays of objects, I can define one dimensional array of object but as I try to define the two dimensional I get a error. What is the correct way to define a multidimensional array of objects in Javascript? Here's my code :

for(var i=0;i<3;i++)
{
   obj1[i] = [
      {property1},{property2}
   ];
   for(var j=0;j<2;j++)
   {
      obj2[i][j]= [
         {property1},{property2}
      ];
   }
}
share|improve this question
1  
I searched google for your question and and it gave me 265,000 viable results in 1/3 of a second. –  Jonny Henly Nov 11 '14 at 3:15
    
possible duplicate of Creating a 2d array of objects in javascript –  Jonny Henly Nov 11 '14 at 3:17
    
First understand how to create 2d arrays in javascript using the link from @Johnny Henly and second learn how to use the object notation here dyn-web.com/tutorials/object-literal. Your syntax is incorrect, you need to provide values for the properties you define –  gabriel Nov 11 '14 at 3:23

2 Answers 2

I think you want:

for (i=0;i<3;i++) {
 f[i]=new Array();
 for (j=0;j<2;j++) {
  f[i][j] = appropriate property ;
 }
}
share|improve this answer

Thanks for all the help, the answer is using it like this:

var obj1 = new Array();
var obj2 = new Array();
for(var i=0;i<3;i++)
{
   obj1[i] = [
      {property1},{property2}
   ];
   var obj2[i] = new Array();
   for(var j=0;j<2;j++)
   {
      obj2[i][j]= [
         {property1},{property2}
      ];
   }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.