-3

I wish to create a multi-dimensional array in the format as:

var sizes = [
        "p" = ["1em", "2em", "3em"],
        "h1" = ["2em", "3em", "4em"]
];

After, I can them loop through sizes and get the array elements for each element....

How is this achievable, tried many ways just doesn't seem to be working. Regards

Alfabravo
  • 7,493
  • 6
  • 46
  • 82
Joshua
  • 588
  • 1
  • 5
  • 19

3 Answers3

1

You can create like this:

var sizes = [
        {"p" : ["1em", "2em", "3em"]},
        {"h1" : ["2em", "3em", "4em"]},
        {"h1" : ["2em", "3em", "4em"]}
];

and you can access it like this:

$(sizes).each(function(index,element){

    element["p"];
   // in element you will find each {}
});
Sudhanshu Jain
  • 494
  • 3
  • 11
0
var sizes = []
sizes["p"] = ["1em", "2em", "3em"];
sizes["h1"] = ["2em", "3em", "4em"];

for (var k in sizes)
   for (var p in sizes[k])
       alert(sizes[k][p])
0

I do not think you want an array, you want an object.

var sizes = {
    p : ["1em", "2em", "3em"],
    h1 : ["2em", "3em", "4em"]
};

Now you can access it with dot notation.

console.log(sizes.p);
console.log(sizes.p[1]);

Than you can just loop over the array

sizes.p.forEach( function(size) {
    console.log(size);
});
epascarello
  • 204,599
  • 20
  • 195
  • 236