...

View Full Version : Setting up and looping through a 3D array?



denhamd2
05-22-2012, 11:08 AM
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?

Old Pedant
05-22-2012, 06:43 PM
Ummm...no, you want to know how to loop through a 2D array.

*THIS* is a ONE dimensional array:


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:


var features = [
[ "headline1", "featureA"],
[ "headline1", "featureB"],
[ "headline1", "featureC"],
[ "headline2", "featureD"],
[ "headline2", "featureE"],
[ "headline2", "featureF"]
];

which works but of course duplicates information. Or is it


var features = {
"headline1" : [ "featureA","featureB","featureC" ],
"headline2" : [ "featureD","featureE","featureF" ]
};
(Notice carefully the change in syntax!)

Or you could even do

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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum