Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2004
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Setting up and looping through a 3D array?

    Hi,

    I'm banging my head off a brick wall with setting up a 3 dimensional array and trying to loop through it. I'm using the EJS framework (http://embeddedjs.com/).

    We currently have a 2D array set up to list out features. See below:

    topfeatures: [
    "Feature 1",
    "Feature 2",
    "Feature 3",
    "Feature 4",
    "Feature 5",
    "Feature 6"
    ]


    <ul>
    [% for(var i = 0; i < this.topfeatures.length; i++) { %]
    <li>[%= this.topfeatures[i] %]</li>
    [% } %]
    </ul>


    However, the request we have requires headlines for each set of features.

    Headline 1
    Feature1
    Feature2
    Feature3

    Headline 2
    Feature4
    Feature5
    Feature6


    Any ideas how I can do this?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,588
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    Ummm...no, you want to know how to loop through a 2D array.

    *THIS* is a ONE dimensional array:
    Code:
    var topfeatures = [ "Feature 1","Feature 2", "Feature 3"];
    But now the answer to the 2D problem depends on how you set up the 2D array.

    Is it like this:
    Code:
    var features = [
         [ "headline1", "featureA"],
         [ "headline1", "featureB"],
         [ "headline1", "featureC"],
         [ "headline2", "featureD"],
         [ "headline2", "featureE"],
         [ "headline2", "featureF"]
    ];
    which works but of course duplicates information. Or is it
    Code:
    var features = {
         "headline1" : [ "featureA","featureB","featureC" ],
         "headline2" : [ "featureD","featureE","featureF" ]
    };
    (Notice carefully the change in syntax!)

    Or you could even do
    Code:
    var features = [
         ["headline1", [ "featureA","featureB","featureC" ] ],
         ["headline2", [ "featureD","featureE","featureF" ] ]
    ];
    So how you do the looping depends on what 2D representation you choose.

    Which will it be?

    NOTE: The 2nd one is most powerful, but I would understand if you didn't want to use it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •