1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2000
    Rep Power

    JavaScript: creating multidimensional arrays 'on the fly'

    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 topic title:

    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

    // 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 prints 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?

  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2001
    Rep Power
    Ditch the multi dimensional arrays and go back to trying the Object Oriented way.

    This is just my opinion, I think once you understand OO javascript you will start writing better code.

    Here is some code to get you started.

    The object
    - is a Theme:
    - has a title
    - has a list of topics

    PHP Code:
    function Theme(title)
    this.title title;
    this.topics = new Array();

    now we need to add a method to add topics to the course

    PHP Code:
    Theme.prototype.addTopic = function(name)
    this.topics[this.topics.length] = name;

    now add code to get the title and topics

    PHP Code:
    Theme.prototype.getTitle = function()
    "<B>" this.title "</B><br>";
    Theme.prototype.getTopics = function()
    for(var x=x<this.topics.length x++)
    str += "....<i>" this.topics[x] + "</i><br>";


    You now have enough code to create an array of themes or courses or whatever

    PHP Code:
    Courses = new Array();

    myCourse = new Theme("Community Issues");
    myCourse.addTopic("The way people relate to food");
    myCourse.addTopic("The role of 'cummunity in the optimum food environment");
    //Add this to the array of courses
    CoursesCourses.length ] = myCourse;

    myCourse = new Theme("Food-related problems");
    myCourse.addTopic("Food Safety");
    myCourse.addTopic("Food Labeling");
    myCourse.addTopic("Policy Concepts");
    //Add this to the array of courses
    CoursesCourses.length ] = myCourse
    then the function to display all this would be

    PHP Code:
    function displayCourses()
    c=c<Courses.length c++)

    The answer does not come from thinking outside the box, it comes from realizing the truth :-
    "There Is No Box".

    JavaScript Gadgets'n'Gizmos

IMN logo majestic logo threadwatch logo seochat tools logo