简体   繁体   中英

prevent nested for loops in javascript

this is my JSON file and i want to create HTML multi level menu without nested loops

[{"text":"Home","href":"http://home.com","icon":"fas fa-home","target":"_top","title":"My Home"},{"text":"Posts","href":"","icon":"fas fa-bell","target":"_self","title":"","children":[{"text":"Sports","href":"","icon":"empty","target":"_self","title":""},{"text":"IT1","href":"","icon":"empty","target":"_self","title":""},{"text":"Web","href":"","icon":"","target":"_self","title":""},{"text":"About","href":"","icon":"fas fa-chart-bar","target":"_self","title":""}]}]

i wrote this but i think its not the best way to do that thanks for helping

        function MenuToHTML(JSON) {
            let html = "<ul>";
            for (items in JSON) {
                html += "<li>";
                //console.log(JSON[items].text);
                html += JSON[items].text;
                if (JSON[items].hasOwnProperty("children")) {
                    var child = JSON[items].children;
                    html += "<ul>";
                    for (subItems in child) {
                        html += "<li>";
                        html += child[subItems].text;
                        if (child[subItems].hasOwnProperty("children")) {
                            html += "<ul>";
                            var child = child[subItems].children;
                            for (SubsubItems in child) {
                                html += "<li>";
                                html += child[SubsubItems].text;
                                html += "</li>";
                            }
                            html += "</ul>";
                        }
                        html += "</li>";
                    }
                    html += "</ul>";
                }
                html += "</li>";
            }
            html += "</ul>";
            return html;
        }

You donot need to create nested loops like that use recursion and for arrays use for for..of or forEach instead of for..in

 let arr = [ {"text":"Home","href":"http://home.com","icon":"fas fa-home","target":"_top","title":"My Home"}, {"text":"Posts","href":"","icon":"fas fa-bell","target":"_self","title":"","children": [{"text":"Sports","href":"","icon":"empty","target":"_self","title":""}, {"text":"IT1","href":"","icon":"empty","target":"_self","title":""}, {"text":"Web","href":"","icon":"","target":"_self","title":""}, {"text":"About","href":"","icon":"fas fa-chart-bar","target":"_self","title":""}]} ] /*this function will convert an array to html list. So you can pass the children array again to the same function and it will to convert children to html list*/ function MenuToHTML(JSON) { let html = "<ul>"; JSON.forEach(item => { html += `<li>${item.text}</li>`; if(item.children){ html += MenuToHTML(item.children); } }) return html + '</ul>'; } document.body.innerHTML = MenuToHTML(arr);

You could build real elements and use a recursion for nested children.

 function getMenu(array) { return array.reduce((ul, { text, href, icon, target, title, children }) => { var li = document.createElement('li'), a; if (href) { a = document.createElement('a'); a.href = href; a.target = target; a.appendChild(document.createTextNode(text)); a.title = title; li.appendChild(a); } else { li.appendChild(document.createTextNode(text)); } if (children) { li.appendChild(getMenu(children)); } ul.appendChild(li); return ul; }, document.createElement('ul')); } var data = [{ text: "Home", href: "http://home.com", icon: "fas fa-home", target: "_top", title: "My Home" }, { text: "Posts", href: "", icon: "fas fa-bell", target: "_self", title: "", children: [{ text: "Sports", href: "", icon: "empty", target: "_self", title: "" }, { text: "IT1", href: "", icon: "empty", target: "_self", title: "" }, { text: "Web", href: "", icon: "", target: "_self", title: "" }, { text: "About", href: "", icon: "fas fa-chart-bar", target: "_self", title: "" }] }]; document.body.appendChild(getMenu(data));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM