-1

Id like to know if theres a way to programmatically create this array in javascript. Id like to have it dynamic also.

var tblObj = {
    main1: {
        var2: var3,
        var3: var4
     },
     main2: {
        var5: var6
     }
};

Thanks

Musa
  • 96,336
  • 17
  • 118
  • 137
JP Rioux
  • 19
  • 1
  • 4
  • 4
    There are no arrays in your question. – Paul Aug 21 '13 at 19:27
  • Don't mind those who votes against your question, it's pretty cool. – Vladislav Qulin Aug 21 '13 at 19:35
  • possible duplicate of [How can I create a two dimensional array in JavaScript?](http://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript) – lightswitch05 Aug 21 '13 at 19:42
  • As you can see from the comments it's not clear what you really want. You mention arrays in your question, but the code shows an object. Maybe you want to read http://eloquentjavascript.net/chapter4.html first to get the terminology right. If you really want to add properties to an object dynamically, then this is a duplicate of http://stackoverflow.com/q/1168807/218196. Posting what you have tried so far to solve the problem might also clear things up. – Felix Kling Aug 21 '13 at 20:00

2 Answers2

1

If you mean an object (as in your example), and want to use variables as keys, you'll have to split the declaration in multiple lines, and use bracket notation:

var tblObj = { main1: {}, main2: {} };
tblObj.main1[var2] = var3;
tblObj.main1[var3] = var4;
tblObj.main2[var5] = var6;

(Assuming all those variables are already defined.)

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • I guess, you don't get the question. It was about strong-typed array. I mean, you can easily set non-array element as property of your object. The point was to avoid possible issues of that kind. – Vladislav Qulin Aug 21 '13 at 19:36
  • Well, I guess now I understand even less, then... I have no idea what you're talking about. Nothing is strongly-typed in js (except for the array buffer views, afaik). – bfavaretto Aug 21 '13 at 19:38
  • Ofc. I mean, the topic starter operates with this category. He wants to use array as two-dimensional array, not as array of arrays. As long as JS is weakly-typed language, you cannot guarantee that every value of first-level array is array itself. So it's not two-dimensional array at all. – Vladislav Qulin Aug 21 '13 at 19:43
  • 1
    @VladislavQulin If you think you know what he's looking for, go ahead and post an answer. I'm still unsure what this question is about, but I made my guess already... – bfavaretto Aug 21 '13 at 19:44
  • I was trying to create a multidimensional JS object tblobj[main1] = {}; tblobj[var1][var2] = var3 Work just fine. Thank for you help ! – JP Rioux Aug 21 '13 at 21:47
0

Sure, you can define a multi-dimensional array in one line, exactly as you are, just using the [] array notation. {} is for objects.

var multidim = [
     [1,2,3],
     [4,5,6],
     [7,8,9]
];
Marc B
  • 356,200
  • 43
  • 426
  • 500