js递归无限层级数据
程序员文章站
2022-05-30 23:21:34
...
//递归结构
var data=[{
"id": 0,
"folder": "sit et quis",
"lower": [{
"id": 1,
"folder": "non est facere"
},
{
"id": 2,
"folder": "alias odio sit 一级B",
"lower": [{
"id": 9,
"folder": "alias odio sit 二级B-1"
},
{
"id": 10,
"folder": "et omnis dolorem 二级B-2",
"lower": [{
"id": 12,
"folder": "et omnis dolorem 三级B-2-1"
},
{
"id": 8,
"folder": " et omnis dolorem 三级B-2-2"
}
]
},
{
"id": 11,
"folder": "provident id volupta 二级B-3"
}
]
}
]
},
{"id": 110,"folder": "quo vero reiciendis"}
];
var strText = '';
//递归
function dg(ml,display){
var d=display ? "display" : "";
strText+="<ul class='"+d+"'>";
for(var i=0;i<ml.length;i++){
strText+="<li ><span>"+ml[i].folder+"</span>";
if(ml[i].lower){
dg(ml[i].lower,true);
}else{
strText+="<ul class='display box'></ul>";
}
strText+="</li>"
}
strText+="</ul>"
return strText;
}
dg(data) //调用
上一篇: Spring RestTemplate中文乱码解决方案
下一篇: VS2019中使用strcpy