学习笔记:函数式编程+递归+数据处理

let categories = [
  {id: 'animals', parent: null},
  {id: 'mammals', parent: 'animals'},
  {id: 'cats', parent: 'mammals'},
  {id: 'dogs', parent: 'mammals'},
  {id: 'dogA', parent: 'dogs'},
  {id: 'dogB', parent: 'dogs'},
  {id: 'catA', parent: 'cats'},
  {id: 'catB', parent: 'cats'},
]

let makeTree = (categories, parent)=>{
  let node = {};
  categories
    .filter(c => c.parent === parent)
    .forEach(c => node[c.id]=
      makeTree(categories, c.id))
  return node;
}
console.log(
  makeTree(categories, null),
  null,
  2
)

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页