I think the best solution is, firstly, to convert your data structure into a tree with parent / child relationships. Then visualization of this structure will be easier, since UL itself has a tree structure.
You can convert menu items using these two functions.
// Add an item node in the tree, at the right position function addToTree( node, treeNodes ) { // Check if the item node should inserted in a subnode for ( var i=0; i<treeNodes.length; i++ ) { var treeNode = treeNodes[i]; // "/store/travel".indexOf( '/store/' ) if ( node.url.indexOf( treeNode.url + '/' ) == 0 ) { addToTree( node, treeNode.children ); // Item node was added, we can quit return; } } // Item node was not added to a subnode, so it a sibling of these treeNodes treeNodes.push({ name: node.name, url: node.url, children: [] }); } //Create the item tree starting from menuItems function createTree( nodes ) { var tree = []; for ( var i=0; i<nodes.length; i++ ) { var node = nodes[i]; addToTree( node, tree ); } return tree; } var menuItemsTree = createTree( menuItems ); console.log( menuItemsTree );
The resulting menuItemsTree will be an object like this.
[ { "name":"Store", "url":"/store", "children":[ { "name":"Travel", "url":"/store/travel", "children":[ ] }, { "name":"Gardening", "url":"/store/gardening", "children":[ ] }, { "name":"Healthy Eating", "url":"/store/healthy-eating", "children":[ { "name":"Cook Books", "url":"/store/healthy-eating/cook-books", "children":[ ] }, { "name":"Single Meal Gifts", "url":"/store/healthy-eating/single-meal-gifts", "children":[ ] } ] }, { "name":"Outdoor Recreation", "url":"/store/outdoor-recreation", "children":[ { "name":"Hiking", "url":"/store/outdoor-recreation/hiking", "children":[ { "name":"Snowshoeing", "url":"/store/outdoor-recreation/hiking/snowshoeing", "children":[ ] } ] }, { "name":"Skiing", "url":"/store/outdoor-recreation/skiing", "children":[ ] } ] }, { "name":"Physical Fitness", "url":"/store/physical-fitness", "children":[ ] }, { "name":"Provident Living", "url":"/store/provident-living", "children":[ ] } ] } ]
You mentioned that you already have html rendering for trees, right? If you need more help, let us know!