-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreeRecursion.js
More file actions
101 lines (97 loc) · 2.35 KB
/
treeRecursion.js
File metadata and controls
101 lines (97 loc) · 2.35 KB
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
// let router = null
function findComponentByPath(routes, path) {
let result = null
let recursion = (routes, path) => {
for (let index = 0; index < routes.length; index++) {
const item = routes[index]
if (item.path === path) {
result = item
break
}
if (item.children && item.children.length > 0) {
recursion(item.children, path)
}
}
}
recursion(routes, path)
return result
}
function newRoutes(routes) {
let result = []
let recursion = (routes, result) => {
let routeIndex = 0
for (let index = 0; index < routes.length; index++) {
const item = routes[index]
const obj = {
path: item.path,
component: item.component,
name: item.name,
meta: item.meta
}
if (!obj.component()) {
continue
}
result[routeIndex] = obj
if (item.children && item.children.length > 0) {
result[routeIndex].children = []
recursion(item.children, result[routeIndex].children)
}
routeIndex++
}
}
recursion(routes, result)
return result
}
const routes = [
{
path: '/permission-manage',
component: () => 1,
redirect: '/permission-manage/permission',
name: 'Permission-manage',
meta: { title: '权限系统' },
children: [
{
path: 'permission',
component: () => 1,
name: 'Permission',
meta: { title: '权限管理' }
},
{
path: 'role',
component: () => null,
name: 'Role',
meta: { title: '角色管理' },
children: [
{
path: 'role_1',
component: () => 1,
name: 'Role_1',
meta: { title: 'role_1' }
},
{
path: 'role_2',
component: () => null,
name: 'Role_2',
meta: { title: 'role_2' }
},
{
path: 'role_3',
component: () => 3,
name: 'Role_3',
meta: { title: 'role_3' }
}
]
},
{
path: 'users',
component: () => null,
name: 'Users',
meta: { title: '用户管理' }
}
]
}
]
let result = findComponentByPath(routes, 'role_2')
let result1 = newRoutes(routes)
console.log(result, '---findComponentByPath')
console.log(JSON.stringify(result1), '---newRoutes')