[英]prevent nested for loops in javascript
this is my JSON file and i want to create HTML multi level menu without nested loops这是我的 JSON 文件,我想创建没有嵌套循环的 HTML 多级菜单
[{"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
您不需要像使用recursion
那样创建嵌套循环,对于arrays
使用for..of
或forEach
而不是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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.