-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree-eg2.js
57 lines (53 loc) · 1.01 KB
/
tree-eg2.js
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
56
57
// 将数据组装起来
const testJson = {
state: "11",
children: [
{
state: "222",
children: [
{
state: "333",
},
{
state: "444",
},
],
},
{
state: "7777",
children: [
{
state: "5555",
},
{
state: "6666",
},
],
},
],
};
let root2;
function register2(path, rootModule) {
let newModule = {
_raw: rootModule,
_children: [],
state: rootModule.state,
};
if (path.length === 0) {
root2 = newModule;
} else {
let parent = path.slice(0, -1).reduce((root, current) => {
return root._children[current];
}, root2);
console.log("newModule", newModule.state);
const curIdx = path[path.length - 1];
parent._children[curIdx] = newModule;
}
if (rootModule.children) {
rootModule.children.forEach((child, idx) => {
register2(path.concat(idx), child);
});
}
}
register2([], testJson);
console.log("root2", root2);