PDA

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



denhamd2
05-22-2012, 09:09 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?

ApacheTech
05-22-2012, 10:03 AM
What language aare you writing this is? PHP, ASP.NET? It's unclear from your post.

EDIT: Just read the embedded Javascript thing. Looks good, but would play havok with ASP.NET. I'll have a look at this some more.

ApacheTech
05-22-2012, 11:41 AM
The best way to do it is to set up an object, then make an array of the objects.



function oCatagory(title, features) {

this.Title = title;
this.Features = function() {
var aReturn = new Array();
for(var i = 0; i < features.length; i++) {
aReturn.push(features[i]);
}
return aReturn;
}
}

var FeatureArray = new Array();
var aLangs = ['PHP', 'ASP.NET', 'SSI'];

FeatureArray[0] = new oCatagory("Languages", aLangs);
FeatureArray[1] = new oCatagory("Search Engines", ['Yahoo', 'Google', 'Ask']);



Then to output...



<% for(var i = 0; i < FeatureArray.length; i++) { %>
<h2><%= FeatureArray[i].Title %></h2>
<ul>
<% for(var j = 0; j < FeatureArray[i].Features.length; j++) { %>
<li><%= FeatureArray[i].Features[j] %></li>
<% } %>
</ul>
<% } %>

ApacheTech
05-22-2012, 11:50 AM
That should output:

Languages

PHP
ASP.NET
SSI


Search Engines

Yahoo
Google
Ask

ApacheTech
05-22-2012, 01:18 PM
As a matter of interest, has anyone tried this EJS Embedded JavaScript Framework in conjuction with ASP.NET? Would it even work?