Method For Storing an N-Depth Tree

Discussion in 'JavaScript' started by HaunahLee, Aug 13, 2008.

  1. #1
    I need to store a tree in a javascript object or array. There is one root element with N children. Each child can have N children, and so on. The last branch node holds a number.

    Real world example:

    
    Hats
        Child
            Blue -> 12
            Green -> 23
            Purple -> 31
            Red -> 14
        Adult
            Black -> 15
            White -> 56
            Gray -> 173
    
    Code (markup):
    I think an associative array would be the best option, but I've read that they aren't supported. What is next best method for storing this type of data?
     
    HaunahLee, Aug 13, 2008 IP
  2. Sleeping Troll

    Sleeping Troll Peon

    Messages:
    217
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Looks like xml to me...
     
    Sleeping Troll, Aug 14, 2008 IP
  3. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    {
    	Hats : {
    		child : {
    			Blue : 12,
    			Green : 23,
    			Purple : 31,
    			Red : 14
    		},
    		Adult : {
    			Blac...etc,
    			etc,
    		}
    	}
    }
    Code (markup):
    JSON; the next best thing since sliced bread.
     
    MMJ, Aug 14, 2008 IP