欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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) //调用