let rootList = [
{id: 1, parent: null,text: "1"},
{id: 11, parent: 1,text: "1-1"},
{id: 12, parent: 1,text: "1-2"},
{id: 2, parent: null,text: "2"},
{id: 21, parent: 2,text: "2-1"},
]
function getTreeList(rootList, id, list){
for(let item of rootList){
if(item.parent === id){
list.push(item)
}
}
for(let i of list){
i.children = []
getTreeList(rootList, i.id, i.children)
if(i.children.length === 0){
delete i.children
}
}
return list
}
console.log(getTreeList(rootList, null, []))
多级数组树状菜单
最新推荐文章于 2024-08-04 16:33:59 发布