parentNodesInTree【tree中获取所有祖先节点】 🔥
描述
根据树状子节点 id, 依次找到所有父节点路径(递归处理) 常用
# 1.示例
import { parentNodesInTree } from 'sf-utils2'
const tree = [
{
id: 1,
name: '香蕉',
pId: null,
children: [
{
id: 1001,
name: '香蕉A',
pId: 1,
children: [
{
id: 1001001,
name: '香蕉A-儿子',
pId: 1001,
children: [
{
id: 1001001001,
name: '香蕉A-儿子-儿子',
pId: 1001001
}
]
}
]
},
{
id: 1002,
name: '香蕉B',
pId: 1,
children: [
{
id: 1002002,
name: '香蕉B-儿子',
pId: 1002
}
]
},
{
id: 1003,
name: '香蕉C',
pId: 1,
children: [
{
id: 1003003,
name: '香蕉C-儿子',
pId: 1003
}
]
}
]
},
{
id: 2,
name: '苹果',
pId: null,
children: [
{
id: 1004,
name: '苹果A',
pId: 2,
children: [
{
id: 1004004,
name: '苹果A-儿子',
pId: 1004
}
]
},
{
id: 1007,
name: '苹果D',
pId: 2
},
{
id: 1008,
name: '苹果D',
pId: 2
},
{
id: 1009,
name: '苹果C',
pId: 2
}
]
},
{
id: 3,
name: '橘子',
pId: null,
children: [
{
id: 1005,
name: '橘子C',
pId: 3,
children: [
{
id: 1005005,
name: '橘子C-儿子',
pId: 1005
}
]
},
{
id: 1006,
name: '橘子B',
pId: 3,
children: [
{
id: 1006006,
name: '橘子B-儿子',
pId: 1006
}
]
},
{
id: 1010,
name: '橘子A',
pId: 3
}
]
},
{
id: 4,
name: '西瓜',
pId: null,
children: [
{
id: 1012,
name: '西瓜B',
pId: 4,
children: [
{
id: 1013,
name: '西瓜B-儿子',
pId: 1012
}
]
}
]
},
{
id: 1011,
name: '西瓜A',
pId: null
}
]
// 根据id是1006006,依次找其上面的 父节点
console.log(parentNodesInTree({ tree: tree, id: 1006006, props: { id: 'id', children: 'children' } }))
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
输出结果,点开查看 👇
;[
{
id: 3,
name: '橘子',
pId: null,
children: [
{
id: 1005,
name: '橘子C',
pId: 3,
children: [
{
id: 1005005,
name: '橘子C-儿子',
pId: 1005
}
]
},
{
id: 1006,
name: '橘子B',
pId: 3,
children: [
{
id: 1006006,
name: '橘子B-儿子',
pId: 1006
}
]
},
{
id: 1010,
name: '橘子A',
pId: 3
}
]
},
{
id: 1006,
name: '橘子B',
pId: 3,
children: [
{
id: 1006006,
name: '橘子B-儿子',
pId: 1006
}
]
},
{
id: 1006006,
name: '橘子B-儿子',
pId: 1006
}
]
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
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
# 2.入参说明
# 主入参
| 参数 | 说明 | 类型 | 是否必填 | 默认值 |
|---|---|---|---|---|
| tree | 原数据,树状结构 | Array | 是 | |
| props | 自定义键名,详情见下 👇 | Object | 否 | |
| id | 要查找的子节点主键值 | String Number | 否 |
# props 对象
| 参数 | 说明 | 类型 | 是否必填 | 默认值 |
|---|---|---|---|---|
| id | 自身主键名,唯一标识 | String | 否 | id |
| children | 树状 children 键名 | String | 否 | children |
# 3.源码
源码,点开查看 👈
import isArray from '@/base/isArray'
/**
* 根据树状子节点id,依次找到所有父节点路径
* @param {Array} tree 原数据,树状结构
* @param {string | number} id 要查找的子节点主键值
* @param {object} props 自定义键名
* @returns {*[]}
*/
function parentNodesInTree({ tree = [], id = '', props = { id: 'id', children: 'children' } }) {
if (!isArray(tree)) return []
const parentNodes = []
const getNodePath = (node, id = '', props) => {
parentNodes.push(node)
if (node?.[props.id] == id) {
throw 'break'
}
if (isArray(node?.[props.children])) {
for (let i = 0; i < node?.[props.children].length; i++) {
getNodePath(node[props.children][i], id, props)
}
}
parentNodes.pop()
}
try {
for (let i = 0; i < tree.length; i++) {
getNodePath(tree[i], id, props)
}
} catch (e) {
return parentNodes
}
}
export default parentNodesInTree
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
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
上次更新: 2023/06/24, 19:35:48