www.webdeveloper.com
Results 1 to 6 of 6

Thread: displaying objects as nested lists

  1. #1
    Join Date
    Jun 2007
    Posts
    3

    displaying objects as nested lists

    Hello,

    I have created the equivalent of a multi dimensional array in javascript using objects.

    I want to then display this information as a nested list but I keep going round in circles getting confused!

    this is an example of the object -

    Code:
    returnArray = new Object;
    returnArray["validationWarnings"] = new Object;
    returnArray["validationWarnings"]["Type"] = new Object;
    returnArray["validationWarnings"]["Type"][0] = "must be set";
    returnArray["validationWarnings"]["Type"][1] = "must be of value - auto, advance, emergency";
    returnArray["validationWarnings"]["Note"] = new Object;
    returnArray["validationWarnings"]["Note"][0] = "must be set";
    returnArray["validationWarnings"]["Note"][1] = "must be less than 100 characters";
    I would like to see output nested, like this -

    validationWarnings
    ----Type
    --------must be set
    --------must be of value - auto, advance, emergency
    ----Note
    --------must be set
    --------must be less than 100 characters

    the actual object is much longer than this, and have objects within objects within objects.

    I keep going round in circles with this! there must be a simple way.

    Thanks.

  2. #2
    Join Date
    Feb 2004
    Location
    London UK
    Posts
    248
    First of all I don't like the way you are building up "returnArray"

    returnArray = new Array();
    returnArray["validationWarnings"] = new Array();
    returnArray["validationWarnings"]["Type"] = new Array();
    returnArray["validationWarnings"]["Type"][0] = "must be set";
    returnArray["validationWarnings"]["Type"][1] = "must be of value - auto, advance, emergency";
    returnArray["validationWarnings"]["Note"] = new Array();
    returnArray["validationWarnings"]["Note"][0] = "must be set";
    returnArray["validationWarnings"]["Note"][1] = "must be less than 100 characters";

    does this help?

  3. #3
    Join Date
    Jun 2007
    Posts
    3
    Thanks for that.

    Not sure why I was using Object instead of Array!

    Now how can I go about outputing the array as a nested list?

  4. #4
    Join Date
    Jun 2007
    Posts
    3
    Quote Originally Posted by nshiell
    First of all I don't like the way you are building up "returnArray"

    returnArray = new Array();
    returnArray["validationWarnings"] = new Array();
    returnArray["validationWarnings"]["Type"] = new Array();
    returnArray["validationWarnings"]["Type"][0] = "must be set";
    returnArray["validationWarnings"]["Type"][1] = "must be of value - auto, advance, emergency";
    returnArray["validationWarnings"]["Note"] = new Array();
    returnArray["validationWarnings"]["Note"][0] = "must be set";
    returnArray["validationWarnings"]["Note"][1] = "must be less than 100 characters";

    does this help?
    Thanks for that.

    Not sure why I was using Object instead of Array!

    Now how can I go about outputing the array as a nested list?

  5. #5
    Join Date
    Feb 2004
    Location
    London UK
    Posts
    248
    I would use a for loop to run through the note and type arrays and build up your list that way

  6. #6
    Join Date
    Jun 2007
    Posts
    8
    Code:
    <script type="text/javascript">
    
    returnArray = new Array();
    returnArray["validationWarnings"] = new Array();
    returnArray["validationWarnings"]["Type"] = new Array();
    returnArray["validationWarnings"]["Type"][0] = "A must be set";
    returnArray["validationWarnings"]["Type"][1] = "must be of value - auto, advance, emergency";
    returnArray["validationWarnings"]["Note"] = new Array();
    returnArray["validationWarnings"]["Note"][0] = "B must be set";
    returnArray["validationWarnings"]["Note"][1] = "must be less than 100 characters";
    
    for(var i=0;i<returnArray["validationWarnings"]["Type"].length;i++){
    	document.write("<ul>");
    		document.write("<li>"+returnArray["validationWarnings"]["Type"][0]+" : "+returnArray["validationWarnings"]["Type"][1]+"</li>");
    	document.write("</ul>");	
    }
    
    </script>
    Output

    Code:
      * A must be set : must be of value - auto, advance, emergency
    
        * A must be set : must be of value - auto, advance, emergency

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles