let arr = [
{
id:"001",
name:'zs',
children:[
{
id:'001-1',
name:'zs-1',
children:[
{
id:'001-1-1',
name:'zs-1-1'
}
]
},
{
id:'001-2',
name:'zs-2',
children:[
{
id:'001-2-1',
name:'zs-2-1'
}
]
}
]
},
{
id:"002",
name:'ls',
children:[
{
id:'002-1',
name:'ls-1',
children:[
{
id:'002-1-1',
name:'ls-1-1'
}
]
},
{
id:'002-2',
name:'ls-2',
children:[
{
id:'002-2-1',
name:'ls-2-1'
}
]
}
]
},
]
function treeToArr(data,pid,res=[]) {
data.forEach(v=>{
res.push({id:v.id,pid:pid,name:v.name})
if(v.children&&v.children.length){
treeToArr(v.children,v.id,res)
}
})
return res
}
console.log(treeToArr(arr))
打印结果: