SHARE
TWEET

Untitled

a guest Aug 17th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let data = [
  2.   {
  3.     title: "menu 1",
  4.     children :[
  5.       { title: "menu 1.1"},
  6.       {
  7.         title: "menu 1.2",
  8.         children: [
  9.           {title: "menu 1.2.1"},
  10.           {title: "menu 1.2.2"},          
  11.         ]
  12.       },
  13.     ]
  14.   },
  15.   {
  16.     title: "menu 2",
  17.     children :[
  18.       { title: "menu 2.1"},
  19.       { title: "menu 2.2"},
  20.     ]
  21.   }
  22. ]
  23.  
  24. function buildTree(data, isChild = false) {
  25.   let html = '<ul>'
  26.   data.forEach(element => {
  27.     html += `<li>${d.title}</li>`
  28.     // If the current data element
  29.     // has children then call the
  30.     // buildTree again passing in
  31.     // the children and isChild = true
  32.     if(d.children) {
  33.       html += buildTree(d.children, true)
  34.     }
  35.   });
  36.   html += '</ul>'
  37.   return html
  38. }
  39.  
  40. let uls = buildTree(data);
  41. console.log(uls);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top