按照HTML结构输出以下data结构
var data = [
{
name:‘AAA‘,
child:[
{name:‘a1‘,child:[{name:‘a1-1‘},{name:‘a1-2‘}]},
{name:‘a2‘},
{name:‘a3‘,child:[{name:‘a3-1‘},{name:‘a3-2‘,child:[{name:‘a3-2-1‘},{name:‘a3-2-2‘}]}]}
]
},
{
name:‘BBB‘,
child:[{name:‘b1‘},{name:‘b2‘},{name:‘b3‘}]
},
{
name:‘CCC‘,
child:[{name:‘c1‘},{name:‘c2‘},{name:‘c3‘}]
}
]
function recursive(data){
var str = ‘<ul>‘
for(var i=0; i<data.length;i++){
str += "<li>" + data[i].name
if(data[i].child){ //递归的结束条件
str += recursive(data[i].child) //递归调用
}
str += ‘</li>‘
}
str += "</ul>"
return str
}
console.log(recursive(data))
递归使代码更加简洁,解决了多层循环的结构。
javascript递归
原文:https://www.cnblogs.com/carry-carry/p/11624401.html