-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
113 lines (112 loc) · 4.13 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>树形数据结构获取最深层数</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id='app'>
<textarea v-model="stringData" cols="100" rows="40">
</textarea>
<button @click="getData()" style="padding: 10px;">
获取
</button>
<div style="text-align: center;">
最大层数:{{maxFloor}}
</div>
</div>
</body>
<script>
var app = new Vue({
el: '#app',
data: {
stringData: `[
{
"label": "广东省",
"children": [
{
"label": "梅州市",
"children": [
{
"label": "兴宁市",
"children": [
{
"label": "黄槐镇",
"children": [
{
"label": "西埔村",
"children": [
{
"label": "中心街",
"children": []
}
]
},
{
"label": "宝龙村",
"children": []
},
{
"label": "双下村",
"children": []
},
{
"label": "双头村",
"children": []
},
{
"label": "槐东村",
"children": []
}
]
}
]
}
]
}
]
},
{"label": "一级2", "children": []},
{"label": "一级3", "children": []}
]`,
maxFloor: 0
},
methods: {
getData () {
let treeData = JSON.parse(this.stringData)
console.log(treeData, 'treeData')
this.maxFloor = this.getMaxFloor(treeData)
},
getMaxFloor (treeData) {
let floor = 0
let v = this
let max = 0
function each (data, floor) {
data.forEach(e => {
e.floor = floor
if (floor > max) {
max = floor
}
if (e.children.length > 0) {
each(e.children, floor + 1)
}
})
}
each(treeData,1)
return max
}
},
mounted: function () {
},
})
</script>
</html>