需要将以下树形结构转成扁平化数组,同时添加一个从父级到子级的路径数组。
let tree = [
{
id: 1,
title: "grandfather",
children: [
{
id: 3,
title: "father"
},
{
id: 4,
title: "father2",
children: [{
id: 6,
title: "child1"
}]
}
]
},
{
id: 2,
name: "grandfather2",
children: [{
id: 5,
name: "father3"
}]
}
];
let list = [
{ id: 1, title: "grandfather", path: ['grandfather'] },
{ id: 3, title: "father", path: ['grandfather', 'father'] },
{ id: 4, title: "father2", path: ['grandfather', 'father2'] },
{ id: 6, title: "child1", path: ['grandfather', 'father2', 'child1'] },
{ id: 2, title: "grandfather", path: ['grandfather2'] },
{ id: 5, title: "father3", path: ['grandfather2', 'father3'] },
];
递归问题实在有点让我头疼,没有能解出来,因此请教一下万能的 V 友有没有什么好的解法。
1
cpstar 2022-06-06 22:51:54 +08:00
深度优先遍历
|
2
sharida 2022-06-06 22:55:17 +08:00 1
递归
```javascript const res = []; function tree2list(tree, path = []) { for (let i = 0; i < tree.length; i++) { const item = tree[i]; const p = [...path, item.title]; res.push({ ...item, path: p }); if (Array.isArray(item.children)) { cb(item.children, p); } } } ``` |
3
cpstar 2022-06-06 23:00:10 +08:00 1
补充 1# 就是递归
array list = []; func recursion(node, parent_path) { list.add({id: node.id, title: node.title, path: parent_path}); for(let n in node.children) recursion(n, parent_path.add(n.title)); } for (let t in tree) { recursion(t, [t.title]); } |
4
luob 2022-06-06 23:23:21 +08:00 1
来点函数式
const h = (list, paths, sum) => list.reduce((acc, { id, title, children }) => ([...acc, { id, title, paths: [...paths, title] }, ...h(children || [], [...paths, title], [])]), sum); const flat = (tree) => h(tree, [], []); |
5
rabbbit 2022-06-06 23:36:42 +08:00 1
const flat = (children) => {
const pathMap = new Map(children.map((i) => [i.id, [i.title]])); const list = [...children]; const result = []; while (list.length > 0) { const { id, title, children } = list.shift(); const path = pathMap.get(id); result.push({ id, title, path }); if (children) { list.unshift(...children); children.forEach((i) => pathMap.set(i.id, [...path, i.title])); } } return result; }; |
6
wunonglin 2022-06-06 23:38:53 +08:00 1
|