makeTree

let cat = [
  {id:'a','paren':null},
  {id:'b','paren':"a"},
  {id:'c','paren':'b'},
  {id:'d','paren':'b'},
  {id:'e','paren':'d'},
  {id:'f','paren':'d'},
  {id:'g','paren':'e'},
]
let makeTree=(cat,paren)=>{
    let node ={}
let aa=cat.filter(c=> c.paren===paren).forEach(c=>
       node[c.id]=makeTree(cat,c.id))
    return node 
}


console.log(
    JSON.stringify(
     makeTree(cat,null),null,2
        )

    )

返回结果

 

posted @ 2019-04-16 19:29  魔都叛徒  阅读(204)  评论(0编辑  收藏  举报