1 treeData: [
2 {
3 id: 10,
4 label: '张三',
5 children: [
6 {
7 id: 11,
8 label: '张三 1-1',
9 children: [
10 {
11 id: 111,
12 label: '张三 1-1-1'
13 },
14 {
15 id: 112,
16 label: '张三 1-1-2'
17 }
18 ]
19 }
20 ]
21 },
22 {
23 id: 20,
24 label: '李四',
25 children: [
26 {
27 id: 21,
28 label: '李四 2-1'
29 },
30 {
31 id: 22,
32 label: '李四 2-2'
33 }
34 ]
35 },
36 {
37 id: 30,
38 label: '王二 3',
39 children: [
40 {
41 id: 31,
42 label: '王二 3-1'
43 },
44 {
45 id: 32,
46 label: '王二 3-2'
47 }
48 ]
49 }
50 ]
1 getIdList(id, tree) {
2 let arr = []
3 for (let i = 0; i < tree.length; i++) {
4 let item = tree[i]
5 arr = []
6 arr.push(item.id)
7 if (id === item.id) {
8 return arr
9 } else {
10 if (item.children && item.children.length > 0) {
11 arr = arr.concat(this.getIdList(id, item.children))
12 if (arr.includes(id)) {
13 return arr
14 }
15 }
16 }
17 }
18 }
1 console.log(this.getIdList(111, this.treeData))
![]()