-2

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}
      ];
   }
}
3
  • 1
    I searched google for your question and and it gave me 265,000 viable results in 1/3 of a second. Commented Nov 11, 2014 at 3:15
  • possible duplicate of Creating a 2d array of objects in javascript Commented Nov 11, 2014 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 Commented Nov 11, 2014 at 3:23

2 Answers 2

1

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 ;
 }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.