image.png

d4706249c0ec021c5dde2c0239c1d97.png

把单个的对象组合成树结构image.png

function Tree(list: any, parentId: any) {
  const parentObj = {} as any;
  list.forEach((o: any) => {
    parentObj[o.id] = o;
  });
  if (!parentId) {
    return list
      .filter((o: any) => !parentObj[o.parentId])
      .map((o: any) => ((o.childes = Tree(list, o.id)), o));
  } else {
    return list
      .filter((o: any) => o.parentId == parentId)
      .map((o: any) => ((o.childes = Tree(list, o.id)), o));
  }
}
最后修改:2022 年 12 月 08 日 03 : 02 PM
如果觉得我的文章对你有用,请随意赞赏