将ArcGIS Server上的图层信息转为Element UI中的Tree控件数据结构:
复制代码
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124let sample = [{ "id": 0, "name": "00", "children": [1, 2] }, { "id": 1, "name": "01", "children": [3, 4] }, { "id": 2, "name": "02", "children": [5] }, { "id": 3, "name": "03", "children": [6] }, { "id": 4, "name": "04", "children": [] }, { "id": 5, "name": "05", "children": [] }, { "id": 6, "name": "06", "children": [] } ] // 返回一个节点的所有子节点 function FindObjectChildTree(objectArrayItem, originObjectArray, idPropName, namePropName, childPropName) { // 如果该子节点无子节点,即到达叶子节点,返回 if (!objectArrayItem[childPropName] || objectArrayItem[childPropName].length == 0) { return { "childTree": { "id": objectArrayItem[idPropName], "name": objectArrayItem[namePropName] }, "checkedID": [objectArrayItem[idPropName]] } } // 否则该节点含有子节点,需要对其子节点做递归 // 注意使用let声明变量,将变量作用域限制在块级,实现闭包 let childTree = { "id": objectArrayItem[idPropName], "name": objectArrayItem[namePropName], "children": [] } let checkedID = [] checkedID.push(objectArrayItem[idPropName]) let childrenIDArray = objectArrayItem[childPropName]; childrenIDArray.forEach(childID => { originObjectArray.forEach(originObject => { if (childID == originObject[idPropName]) { const findResult = FindObjectChildTree(originObject, originObjectArray, idPropName, namePropName, childPropName); childTree.children.push(findResult["childTree"]); checkedID.push(...findResult["checkedID"]) } }) }); return { "childTree": childTree, "checkedID": checkedID }; } let childTree = [] let checkID = [] sample.forEach(element => { id = element.id if (checkID.indexOf(id) == -1) { checkID.push(id) const findResult = FindObjectChildTree(element, sample, "id", "name", "children") childTree.push(findResult.childTree) checkID.push(...findResult.checkedID) } }); console.log(JSON.stringify(childTree)) [{ "id": 0, "name": "00", "children": [{ "id": 1, "name": "01", "children": [{ "id": 3, "name": "03", "children": [{ "id": 6, "name": "06" }] }, { "id": 4, "name": "04" }] }, { "id": 2, "name": "02", "children": [{ "id": 5, "name": "05" }] }] }]
最后
以上就是忧郁棒棒糖最近收集整理的关于【Javascript】使用递归将嵌套对象数组转为树状嵌套结构的全部内容,更多相关【Javascript】使用递归将嵌套对象数组转为树状嵌套结构内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复