childNodesInList【列表中获取某个下所有后代节点】🔥
描述
从平铺化中的 list,根据父节点 id 获取其下所有的子节点、后代子节点(递归处理) 常用
# 1.示例
import { childNodesInList } from 'sf-utils2'
const list = [
{ id: 1, name: '香蕉', pId: null },
{ id: 2, name: '苹果', pId: null },
{ id: 3, name: '橘子', pId: null },
{ id: 1001, name: '香蕉A', pId: 1 },
{ id: 1002, name: '香蕉B', pId: 1 },
{ id: 1003, name: '香蕉C', pId: 1 },
{ id: 1004, name: '苹果A', pId: 2 },
{ id: 1005, name: '橘子C', pId: 3 },
{ id: 1006, name: '橘子B', pId: 3 },
{ id: 1007, name: '苹果D', pId: 2 },
{ id: 1008, name: '苹果D', pId: 2 },
{ id: 1009, name: '苹果C', pId: 2 },
{ id: 1010, name: '橘子A', pId: 3 },
{ id: 1001001, name: '香蕉A-儿子', pId: 1001 },
{ id: 1002002, name: '香蕉B-儿子', pId: 1002 },
{ id: 1003003, name: '香蕉C-儿子', pId: 1003 },
{ id: 1004004, name: '苹果A-儿子', pId: 1004 },
{ id: 1005005, name: '橘子C-儿子', pId: 1005 },
{ id: 1006006, name: '橘子B-儿子', pId: 1006 },
{ id: 4, name: '西瓜', pId: null },
{ id: 1011, name: '西瓜A', pId: null },
{ id: 1012, name: '西瓜B', pId: 4 },
{ id: 1013, name: '西瓜B-儿子', pId: 1012 },
{ id: 1001001001, name: '香蕉A-儿子-儿子', pId: 1001001 }
]
// 获取 parentId为4,即,找到西瓜下所有的子节点信息
childNodesInList({ list: list, parentId: 4, props: { id: 'id', parentId: 'pId' } })
// 结果
// [
// { id: 1012, name: '西瓜B', pId: 4 },
// { id: 1013, name: '西瓜B-儿子', pId: 1012 }
// ]
// 获取 parentId为4,即,找到西瓜下所有的子节点信息,只返回id组成的数组
childNodesInList({ list: list, parentId: 4, props: { id: 'id', parentId: 'pId' }, returnType: 'id' })
// 结果
// [ 1012, 1013 ]
// 获取 parentId为1,即,找到香蕉下所有的子节点信息
childNodesInList({ list: list, parentId: 1, props: { id: 'id', parentId: 'pId' } })
// 结果
// [
// { id: 1001, name: '香蕉A', pId: 1 },
// { id: 1001001, name: '香蕉A-儿子', pId: 1001 },
// { id: 1001001001, name: '香蕉A-儿子-儿子', pId: 1001001 },
// { id: 1002, name: '香蕉B', pId: 1 },
// { id: 1002002, name: '香蕉B-儿子', pId: 1002 },
// { id: 1003, name: '香蕉C', pId: 1 },
// { id: 1003003, name: '香蕉C-儿子', pId: 1003 }
// ]
// 获取 parentId为1001001,即,找到香蕉香蕉A-儿子下所有的子节点信息
childNodesInList({ list: list, parentId: 1001001, props: { id: 'id', parentId: 'pId' } })
// 结果
// [
// { id: 1001001001, name: '香蕉A-儿子-儿子', pId: 1001001 }
// ]
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
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
# 2.入参说明
# 主入参
| 参数 | 说明 | 类型 | 是否必填 | 默认值 |
|---|---|---|---|---|
| list | 原数据(平铺化数据 list) | Array | 是 | |
| props | 自定义字段属性名,详情见下 👇 | Object | 否 | |
| parentId | 父节点 id | String | Number | 否 | 0 |
| returnType | 返回类型是以 id 为结果的数组 还是 以子节点整体对象为结果的数组。item:子节点具体信息 id:只保留子节点 id, 可选值: item、id | String | 否 | item |
# props 对象
| 参数 | 说明 | 类型 | 是否必填 | 默认值 |
|---|---|---|---|---|
| id | 自身主键名,唯一标识 | String Number | 否 | id |
| parentId | 父节点键名 | String | 否 | parentId |
# 3.源码
源码,点开查看 👈
import isArray from '@/base/isArray'
import arrayToObj from '@/array/arrayToObj'
/**
* 根据 父节点id 获取其 所有子节点信息
* @param {Array} list 列表
* @param {string | number} parentId 父节点唯一标识值
* @param {object} props 数据字段映射
* @param {String} returnType 返回数据类型 可选值 item:子节点具体信息 id:只保留子节点id, 默认值item
* @returns {*[]}
*/
function childNodesInList({
list = [],
parentId = 0,
props = { id: 'id', parentId: 'parentId' },
returnType = 'item' // item:子节点具体信息 id:只保留子节点id
}) {
const childIds = []
const childNodesInListFn = ({
list = [],
parentId = 0,
props = { id: 'id', parentId: 'parentId' },
childIds = [],
returnType = 'item' // item:子节点具体信息 id:只保留子节点id
}) => {
if (isArray(list) && list.length) {
list.forEach(item => {
if (item[props.parentId] == parentId) {
childIds.push(item[props.id])
childIds = [
...new Set([
...childIds,
...childNodesInListFn({
list,
parentId: item[props.id],
props,
childIds,
returnType
})
])
]
}
})
}
return childIds
}
const ids = childNodesInListFn({
list,
parentId,
props,
childIds,
returnType // item:子节点具体信息 id:只保留子节点id
})
const map = new Map([
[
'item',
() => {
const listToObject = arrayToObj(list, props.id)
return ids.map(v => listToObject[v])
}
],
[
'id',
() => {
return ids
}
]
])
return map.get(returnType)?.()
}
export default childNodesInList
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
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
上次更新: 2023/06/24, 19:35:48