Google Answers Logo
View Question
 
Q: Javascript - function to convert data structures from 'flat' to hierarchical ( No Answer,   1 Comment )
Question  
Subject: Javascript - function to convert data structures from 'flat' to hierarchical
Category: Computers > Programming
Asked by: southof41-ga
List Price: $50.00
Posted: 16 May 2006 06:14 PDT
Expires: 17 May 2006 18:08 PDT
Question ID: 729312
Write a JS function to convert an array like this ...

arr_Menu_Driver = [
['USA','','','','',''],
['Russia','/countries?id=801','Moscow','','',''],
['Russia','/countries?id=801','St Petersburg','/cities?id=97','Nevsky
Prospekt','/streets?id=5'],
['UK','/countries?id=802','London','/cities?id=96', 'Oxford
Street','/streets?id=6'],
['UK','/countries?id=802','London','/cities?id=96','Piccadilly','/streets?id=7'],
['UK','/countries?id=802','Edinburgh','/cities?id=95','Princes Street','']
]

into a structure like this....

var MENU_ITEMS = [
        {"popup":,'/streets?id=802'1, "popupoff":[0,0], "pos":[0,0],
"size":[20,70], "itemoff":[19,0], "leveloff":[10,59], "delay":500,
"style":STYLE},
        {code:"USA",style:DISABLEDMENUELEMENT},
        {code:"Russia",url:"/countries?id=801"
                sub:[
                        {code:"Moscow",style:DISABLEDMENUELEMENT},
                        {code:"St Petersburg",url:"/cities?id=97"}
                        sub:[
                            {"itemoff":[21,0]},
                            {code:"Nevsky Prospekt", "url":"/streets?id=5"}
                        ]},
                ]},
        },
        {code:"UK",url:"/countries?id=802"
                sub:[
                        {code:"London",url:"/cities?id=96"
                        sub:[
                            {"itemoff":[21,0]},
                            {code:"Oxford Street", "url":"/streets?id=6"},
                            {code:"Piccadilly Circus", "url":"/streets?id=7"}
                        ]},
                        {code:"Edinburgh",url:"/cities?id=95"}
                        sub:[
                            {"itemoff":[21,0]},
                            {code:"Princes Street", style:DISABLEDMENUELEMENT}
                        ]},
                ]},
        },
]

... the MENU_ITEMS structure is one used by CoolDev Menuing system
(see http://javascript.cooldev.com/doc/menu/ for details).

It's worth noting that where an element in the input array has no URL
this results in that menu element being 'disabled' in the sense that
(A) it has not child menu elements and (B) the attribute 'style' is
set to DISABLEDMENUELEMENT.

To start with it might make it easier to illustrate the requirement
with an input array that has no disabled elements so here goes with
one that is all enabled...

arr_Menu_Driver = [
['USA','/countries?id=800','New York','/cities?id=99','Fifth
Avenue','/streets?id=0'],
['USA','/countries?id=800','New York','/cities?id=99','Wall
Street','/streets?id=1'],
['USA','/countries?id=800','Washington
DC','/cities?id=999','Pennsylvania Avenue','/streets?id=2'],
['Russia','/countries?id=801','Moscow','/cities?id=98','Tverskaya
Ulitsa','/streets?id=3'],
['Russia','/countries?id=801','Moscow','/cities?id=98','Kuznetsky
Most','/streets?id=4'],
['Russia','/countries?id=801','St Petersburg','/cities?id=97','Nevsky
Prospekt','/streets?id=5'],
['UK','/countries?id=802','London','/cities?id=96', 'Oxford
Street','/streets?id=6'],
['UK','/countries?id=802','London','/cities?id=96','Piccadilly','/streets?id=7'],
['UK','/countries?id=802','Edinburgh','/cities?id=95','Princes
Street','/streets?id=8']
]

var MENU_ITEMS = [
        {"popup":,'/streets?id=802'1, "popupoff":[0,0], "pos":[0,0],
"size":[20,70], "itemoff":[19,0], "leveloff":[10,59], "delay":500,
"style":STYLE},
        {code:"USA",url:"/countries?id=800",
                sub:[
                        {code:"New York",url:"/streets?id=99"
                        sub:[
                            {"itemoff":[21,0]},
                            {code:"Fifth Avenue", "url":"/streets?id=0"},
                            {code:"Wall Street", "url":"/streets?id=1"}
                        ]},
                        {code:"Washington",url:"/streets?id=999"}
                        sub:[
                            {"itemoff":[21,0]},
                            {code:"Pennsylvania Avenue", "url":"/streets?id=0"}
                        ]},
                ]},
        {code:"Russia",url:"/countries?id=801"
                sub:[
                        {code:"Moscow",url:"/cities?id=98"
                        sub:[
                            {"itemoff":[21,0]},
                            {code:"Tverskaya Ulitsa", "url":"/streets?id=3"},
                            {code:"Kuznetsky Most", "url":"/streets?id=4"}
                        ]},
                        {code:"St Petersburg",url:"/cities?id=97"}
                        sub:[
                            {"itemoff":[21,0]},
                            {code:"Nevsky Prospekt", "url":"/streets?id=5"}
                        ]},
                ]},
        },
        {code:"UK",url:"/countries?id=802"
                sub:[
                        {code:"London",url:"/cities?id=96"
                        sub:[
                            {"itemoff":[21,0]},
                            {code:"Oxford Street", "url":"/streets?id=6"},
                            {code:"Piccadilly Circus", "url":"/streets?id=7"}
                        ]},
                        {code:"Edinburgh",url:"/cities?id=95"}
                        sub:[
                            {"itemoff":[21,0]},
                            {code:"Princes Street", "url":"/streets?id=8"}
                        ]},
                ]},
        },
]


Please ask questions if this is not clear.
Answer  
There is no answer at this time.

Comments  
Subject: Re: Javascript - function to convert data structures from 'flat' to hierarchical
From: roonaan-ga on 17 May 2006 11:18 PDT
 
Hi,

A basic setup of the function you requested is listed below.

I hope this is what you are looking for and works in your situation as
well as it did in the testcases I worked with.

Kind regards,

-r-

    arr_Menu_Driver = [
      ['USA','','New York','/cities?id=99','Fifth Avenue','/streets?id=0'],
      ['USA','','New York','/cities?id=99','Wall Street','/streets?id=1'],
      ['USA','','Washington DC','/cities?id=999','Pennsylvania
Avenue','/streets?id=2'],
      ['Russia','/countries?id=801','Moscow','','Tverskaya
Ulitsa','/streets?id=3'],
      ['Russia','/countries?id=801','Moscow','','Kuznetsky Most','/streets?id=4'],
      ['Russia','/countries?id=801','St
Petersburg','/cities?id=97','Nevsky Prospekt','/streets?id=5'],
      ['UK','/countries?id=802','London','/cities?id=96', 'Oxford
Street','/streets?id=6'],
      ['UK','/countries?id=802','London','/cities?id=96','Piccadilly','/streets?id=7'],
      ['UK','/countries?id=802','Edinburgh','','Princes Street','/streets?id=8']
    ]

function MenuStruct(arr) {
      var root = {};
      root.sub = [];
      root.items = {};
      for(i = 0; i< arr.length; i++) {
        var lvl1  = arr[i][0];
        var url1  = arr[i][1];
        var lvl2  = arr[i][2];
        var url2  = arr[i][3];
        var lvl3  = arr[i][4];
        var url3  = arr[i][5];
        if(lvl1) {
          if(!root.items[lvl1]) {
            root.items[lvl1] = root.sub.length;
            var newelem = {code:lvl1, url:url1, format:{style:STYLE},
items:{}, sub:[{itemoff:[21,0]}]};
            if(url1.length < 1) {
              newelem.format = {style:DISABLEDMENUELEMENT};
            }
            root.sub.push(newelem);
          }
          if(lvl2) {
            index1 = root.items[lvl1];
            elem1  = root.sub[index1];
            if(!elem1.items[lvl2]) {
              elem1.items[lvl2] = elem1.sub.length;
              var newelem2 = {code:lvl2, url:url2, items:{},
format:{style:STYLE}, sub:[{leveloff:[5,90]}]};
              if(url2.length < 1) {
                newelem2.format = {style:DISABLEDMENUELEMENT};
              }
              elem1.sub.push(newelem2);
            }
            if(lvl3) {
              index2 = elem1.items[lvl2];
              elem2  = elem1.sub[index2];
              if(!elem2.items[lvl3]) {
                elem2.items[lvl3] = elem1.sub.length;
                var newelem3 = {code:lvl3, url:url3, format:{style:STYLE}}
                if(url3.length < 1) {
                  newelem3.format = {style:DISABLEDMENUELEMENT};
                }
                elem2.sub.push(newelem3);
              }
            } //end (if lvl3)
          } //end if(lvl2)
        } //end if(lvl1)
      }
      var result = new Array();
      result.push({pos:[10,10], itemoff:[0,99], leveloff:[21,0],
style:STYLE, size:[22,100]});
      for(i in root.sub) {
        elem = root.sub[i];
        tree = {code:elem.code, sub:elem.sub, format:elem.format};
        result.push(tree);
      }
      return result;
    }
    var MENU_ITEMS = MenuStruct(arr_Menu_Driver);
    var m1 = new COOLjsMenu("menu1", MENU_ITEMS)

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy