Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let data = [
- {
- title: "menu 1",
- children :[
- { title: "menu 1.1"},
- {
- title: "menu 1.2",
- children: [
- {title: "menu 1.2.1"},
- {title: "menu 1.2.2"},
- ]
- },
- ]
- },
- {
- title: "menu 2",
- children :[
- { title: "menu 2.1"},
- { title: "menu 2.2"},
- ]
- }
- ]
- function buildTree(data, isChild = false) {
- let html = '<ul>'
- data.forEach(element => {
- html += `<li>${d.title}</li>`
- // If the current data element
- // has children then call the
- // buildTree again passing in
- // the children and isChild = true
- if(d.children) {
- html += buildTree(d.children, true)
- }
- });
- html += '</ul>'
- return html
- }
- let uls = buildTree(data);
- console.log(uls);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement