I wanted create a three dimensional array on the fly. In this example, I create a class to encapsulate an array of pro sports teams.
function ClassTeams ()
{
this.aaastrTeamsByLeagueCityYear = [];
// to add element to array
function addTeam (strLeague, strCity, nYear, strTeam)
{
if ((typeof this.aaastrTeamsByLeagueCityYear[strLeague]) === "undefined")
{ // first element from this league. Need to create array.
this.aaastrTeamsByLeagueCityYear[strLeague] = [];
}
if ((typeof this.aaastrTeamsByLeagueCityYear[strLeague][strCity]) === "undefined")
{ // first element for this city in this league. Need to create array.
this.aaastrTeamsByLeagueCityYear[strLeague][strCity] = [];
}
this.aaastrTeamsByLeagueCityYear[strLeague][strCity][nYear] = strTeam;
};
// get element from array
function getTeam (strLeague, strCity, nYear)
{
return (this.aaastrTeamsByLeagueCityYear[strLeague][strCity][nYear]);
};
// list all elements in array
function showTeams ()
{
var strLeague;
var strCity;
var nYear;
var strHtml="<table>";
strHtml += "<tr>";
strHtml += "<th>League</th>";
strHtml += "<th>City</th>";
strHtml += "<th>Year</th>";
strHtml += "<th>Team</th>";
strHtml += "</tr>";
for (strLeague in this.aaastrTeamsByLeagueCityYear);
{
for (strCity in this.aaastrTeamsByLeagueCityYear[strLeague]);
{
for (nYear in this.aaastrTeamsByLeagueCityYear[strLeague][strCity]);
{
strHtml += "<tr>";
strHtml += "<td>" + strLeague + "</td>";
strHtml += "<td>" + strCity + "</td>";
strHtml += "<td>" + nYear + "</td>";
strHtml += "<td>" + this.aaastrTeamsByLeagueCityYear[strLeague][strCity][nYear] + "</td>";
strHtml += "</tr>";
}
}
}
strHtml+="</table>";
return (strHtml);
};
// create function to show all teams in given league
// because League is in the primary array, this is relatively easy
this.showLeagueTeams = function (strLeague)
{
// don't need local variable to traverse League
var strCity;
var nYear;
var strHtml="<table>";
strHtml += "<tr>";
strHtml += "<th>League</th>";
strHtml += "<th>City</th>";
strHtml += "<th>Year</th>";
strHtml += "<th>Team</th>";
strHtml += "</tr>";
// no need to traverse primary array, array of leagues.
for (strCity in this.aaastrTeamsByLeagueCityYear[strLeague]);
{
for (nYear in this.aaastrTeamsByLeagueCityYear[strLeague][strCity]);
{
strHtml += "<tr>";
strHtml += "<td>" + strLeague + "</td>";
strHtml += "<td>" + strCity + "</td>";
strHtml += "<td>" + nYear + "</td>";
strHtml += "<td>" + this.aaastrTeamsByLeagueCityYear[strLeague][strCity][nYear] + "</td>";
strHtml += "</tr>";
}
}
strHtml+="</table>";
return (strHtml);
};
// create function to show all teams for a given city
// because City is in a secondary array, this is a little more complicated
this.showCityTeams = function (strMyCity)
{
var strLeague;
var strCity; // do need local variable to traverse cities
var nYear;
var strHtml="<table>";
strHtml += "<tr>";
strHtml += "<th>League</th>";
strHtml += "<th>City</th>";
strHtml += "<th>Year</th>";
strHtml += "<th>Team</th>";
strHtml += "</tr>";
for (strLeague in this.aaastrTeamsByLeagueCityYear);
{
for (strCity in this.aaastrTeamsByLeagueCityYear[strLeague]);
{ // check all the cities of the given league
if (strCity == strMyCity)
{ // this is where we limit display to particular city
for (nYear in this.aaastrTeamsByLeagueCityYear[strLeague][strCity]);
{
strHtml += "<tr>";
strHtml += "<td>" + strLeague + "</td>";
strHtml += "<td>" + strCity + "</td>";
strHtml += "<td>" + nYear + "</td>";
strHtml += "<td>" + this.aaastrTeamsByLeagueCityYear[strLeague][strCity][nYear] + "</td>";
strHtml += "</tr>";
}
}
}
}
strHtml+="</table>";
return (strHtml);
};
}
use it
var objTeams = new ClassTeams ();
objTeams.addTeam ("NFL","Detroit",2012,"Lions");
objTeams.addTeam ("MLB","Detroit",2012,"Tigers");
objTeams.addTeam ("NFL","Chicago",2012,"Bears");
objTeams.showTeams (); // show all 3 teams
objTeams.showLeagueTeams ("NFL"); // show both NFL teams
objTeams.showCityTeams ("Detroit"); // show both Detroit teams
// etc