JavaScript 树形结构数据平铺展开


代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// 原来的树形结构
const oldTreeData = [
{
name: "总部",
place: "一楼",
children: [
{
name: "财务部",
place: "二楼",
},
{
name: "生产部",
place: "三楼",
},
{
name: "开发部",
place: "三楼",
children: [
{
name: "软件部",
place: "四楼",
children: [
{ name: "后端部", place: "五楼" },
{ name: "前端部", place: "七楼" },
{ name: "技术部", place: "六楼" },
],
},
{
name: "硬件部",
place: "四楼",
children: [
{ name: "DSP部", place: "八楼" },
{ name: "ARM部", place: "二楼" },
{ name: "调试部", place: "三楼" },
],
},
],
},
],
},
];

// 树形结构平铺
function treeDataAdapter(treeData, lastArrayData = []) {
treeData.forEach((item) => {
if (item.children) {
treeDataAdapter(item.children, lastArrayData);
}
const { name, place } = item;
lastArrayData.push({ name, place });
});
return lastArrayData;
}
// 返回平铺的组织结构
var data = treeDataAdapter(oldTreeData);

Reference

JavaScript 设计模式学习第十三篇-适配器模式