递归算法
function fn(tree) {
let str = '<ul>'
tree.forEach(item => {
str += '<li>' + item.name + (item.child ? fn(item.child) : '') + '</li>'
})
return str + '</ul>'
}
document.body.innerHTML = fn(menudata)
目录结构
var menudata = [
{
name: 'AAA',
child: [{
name: 'a1',
child: [{
name: 'a1-1'
}, {
name: 'a1-2'
}]
}
]
},
{
name: 'BBB',
child: [{
name: 'b1',
child: [{
name: 'b1-1'
}, {
name: 'b1-2'
}]
}]
},
{
name: 'CCC',
child: [{
name: 'c1'
}]
}
];