Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: Creating Multidimensional Arrays 'on the fly'


Message #1 by simonds@m... on Wed, 8 May 2002 17:10:47
I am trying to do something that I'm not sure will work.
I want to have arrays created on the fly without having to declare them 
each time. In other words, I want to code their declaration automatically.

I have a course with individual themes (each with its own title), and 
within each theme there are any number of topics which have a title. I 
want to write the theme title followed by the 

e.g., using the data below:
(Theme 1, Topic 1)

Community Issues: The way people relate to food

I want to reuse this script, but don't know how many topics will be 
associated with each theme.

***************************
Theme = new Array();
Topics = new Array();

// this is the part that I want to do automatically with the function below
/*
Topics[1] = new Array();
Topics[2] = new Array();
Topics[3] = new Array();
Topics[4] = new Array();
Topics[5] = new Array();
*/

// Assign the title to the theme
Theme[1] = "Community Issues";

Theme[2] = "Food-related Problems";

Theme[3] = "Economic Issues";;

Theme[4] = "Health Systems";

Theme[5] = "Policy Applications";

// this sets up the writing of the Theme title
var ThemeTitle;
var i;

function DisplayThemeTitle() {
  for (i = 1; i <= Theme.length; i++) {
    if (ThemeNum == i) {
 	ThemeTitle = document.write("<b>" + Theme[i] + ": ");
    } // close if 'ThemeNum' statement
  } // close for 'Theme' statement
return ThemeTitle;
} // close DisplayThemeTitle function


// this is where I want to create the topics for each theme programatically
var y = 1;
var z;
function CreateTopics(oCourseData) {
//  while (y <= Theme.length) {
    for (z = 1; z <= Topics.length; z++) {
      Topics[ThemeNum] = new Array();
    } //end for statement
//  } // end while loop
  return Topics[z];
} // close function
CreateTopics();

// I then can just declare the title of each topic; the first part of the 
array relates to the corresponding theme number
Topics[1][1] =  "The way people relate to food";
Topics[1][2] =  "The role of 'community' in an optimum food environment";
Topics[1][3] =  "Views about responsibility for a healthy population";
Topics[1][4] =  "Individual or community oriented programs";
Topics[1][5] = "Political actions in communities for nutrition problems";

Topics[2][1] = ("Protection");
Topics[2][2] = ("Food safety");
Topics[2][3] = ("Food labeling");
Topics[2][4] = ("Policy concepts");
Topics[2][5] = ("Multiple causes of nutrition problems");
Topics[2][6] = ("Causal mode");

// this gets the topic title and [rints it to the document
var x;
var TopicTitle;;

function DisplayTopicTitle() {
      for (x = 1; x <= Topics.length; x++) {
        if (TopicNum == x) {
	  TopicTitle = document.write(Topics[ThemeNum][x] + '</b>');
	} // close if 'TopicNum' statement
      } // close for 'Topic' statement
return TopicTitle;
}
**********************************

I get the error that Topic[1] is undefined. Note that some info comes from 
a previous script put in above this one in the html document. I then have 
a header.js script right after the <body> tag that calls the two functions 
(DisplayThemeTitle() and DisplayTopicTitle)

I tried to do it as an object but was unsuccessful; e.g.,:
oCourseData = new Object(Topics);

Any suggestions?

Regards,
Eric

  Return to Index