matchSubListGroup
描述
多个列表 进行分组匹配子列表, 形成复杂嵌套结构。可能说的不太明白,点击 👇
# 1.示例
例子
比如有个 工作 jobs 岗位列表(基础信息)、 工作详情 jobContent 列表、工作类型 jobType 列表,工作地点列表 jobPlace 列表。
要求:
1、将 jobContent 列表、 jobType 列表 合并到 jobs岗位列表中,生成嵌套的子列表解构,
2、将 jobPlace 列表 合并到 jobContent 岗位列表中,生成嵌套的子列表解构,
import { matchSubListGroup } from 'sf-utils2'
const jobs = [
// 工作
{ id: 56539, job: '培训运营实习生', type: '市场营销/人力资源管理', comp: '复星集团' },
{ id: 56538, job: '人事资源实习生', type: '人力资源管理/MBA', comp: '金发科技' },
{ id: 56537, job: '人工智能中心产品经理实习生', type: '职位要求', comp: '平安科技' }
]
const jobContent = [
// 工作详情
{ id: 12024, content: '1.全日制本科及以上在校生,人力资源管理、市场营销、档案管理等相关专业优先。', jobId: 56539 },
{ id: 12025, content: '2.思维灵活,积极主动地与人沟通,具备踏实、认真的工作态度。', jobId: 56539 },
{ id: 12026, content: '3.良好的英语应用及文案写作能力,文笔好为佳。', jobId: 56539 },
{
id: 12027,
content: '1、协助公司相关岗位招聘实施工作,包括:简历筛选、电话/线上沟通人选意向、参与核心面试环节等;',
jobId: 56538
},
{ id: 12028, content: '2、协助部门其他模块事项,如培训或项目型工作。', jobId: 56538 },
{ id: 12029, content: '- 项目交付:协助进行需求对接、项目管理、制作文档等工作。', jobId: 56537 },
{ id: 12030, content: '- 产品设计:协助进行页面设计、交互设计、竞品分析等工作。', jobId: 56537 },
{ id: 12031, content: '- 其他支持:按需支持行业调研与对外推广等工作。', jobId: 56537 }
]
const jobPlace = [
// 工作地点
{ id: 13001, city: '北京', jobContentId: 12025 },
{ id: 13002, city: '上海', jobContentId: 12025 },
{ id: 13003, city: '南京', jobContentId: 12025 },
{ id: 13004, city: '连云港', jobContentId: 12027 },
{ id: 13005, city: '无锡', jobContentId: 12027 },
{ id: 13006, city: '杭州', jobContentId: 12027 }
]
const jobType = [
// 工作类型
{ id: 801, city: '市场营销', jobId: 56539 },
{ id: 801, city: '人力资源管理', jobId: 56539 },
{ id: 802, city: '人力资源管理', jobId: 12026 },
{ id: 802, city: 'MBA', jobId: 12026 },
{ id: 803, city: '会计', jobId: 56537 },
{ id: 803, city: '法学', jobId: 56537 }
]
const result = matchSubListGroup([
{ parent: { list: jobs, id: 'id' }, child: { list: jobContent, pId: 'jobId' }, fieldName: 'jobContentList' },
{ parent: { list: jobs, id: 'id' }, child: { list: jobType, pId: 'jobId' }, fieldName: 'jobTypeList' },
{ parent: { list: jobContent, id: 'id' }, child: { list: jobPlace, pId: 'jobContentId' }, fieldName: 'jobPlaceList' }
])
console.log(result)
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
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
输出结果,点击查看
;[
{
id: 56539,
job: '培训运营实习生',
type: '市场营销/人力资源管理',
comp: '复星集团',
jobContentList: [
// 这就是合并后新增的字段列表
{
id: 12024,
content: '1.全日制本科及以上在校生,人力资源管理、市场营销、档案管理等相关专业优先。',
jobId: 56539,
jobPlaceList: [] // 这就是合并后新增的字段列表
},
{
id: 12025,
content: '2.思维灵活,积极主动地与人沟通,具备踏实、认真的工作态度。',
jobId: 56539,
jobPlaceList: [
// 这就是合并后新增的字段列表
{
id: 13001,
city: '北京',
jobContentId: 12025
},
{
id: 13002,
city: '上海',
jobContentId: 12025
},
{
id: 13003,
city: '南京',
jobContentId: 12025
}
]
},
{
id: 12026,
content: '3.良好的英语应用及文案写作能力,文笔好为佳。',
jobId: 56539,
jobPlaceList: []
}
],
jobTypeList: [
{
id: 801,
city: '市场营销',
jobId: 56539
},
{
id: 801,
city: '人力资源管理',
jobId: 56539
}
]
},
{
id: 56538,
job: '人事资源实习生',
type: '人力资源管理/MBA',
comp: '金发科技',
jobContentList: [
{
id: 12027,
content: '1、协助公司相关岗位招聘实施工作,包括:简历筛选、电话/线上沟通人选意向、参与核心面试环节等;',
jobId: 56538,
jobPlaceList: [
{
id: 13004,
city: '连云港',
jobContentId: 12027
},
{
id: 13005,
city: '无锡',
jobContentId: 12027
},
{
id: 13006,
city: '杭州',
jobContentId: 12027
}
]
},
{
id: 12028,
content: '2、协助部门其他模块事项,如培训或项目型工作。',
jobId: 56538,
jobPlaceList: []
}
],
jobTypeList: []
},
{
id: 56537,
job: '人工智能中心产品经理实习生',
type: '职位要求',
comp: '平安科技',
jobContentList: [
{
id: 12029,
content: '- 项目交付:协助进行需求对接、项目管理、制作文档等工作。',
jobId: 56537,
jobPlaceList: []
},
{
id: 12030,
content: '- 产品设计:协助进行页面设计、交互设计、竞品分析等工作。',
jobId: 56537,
jobPlaceList: []
},
{
id: 12031,
content: '- 其他支持:按需支持行业调研与对外推广等工作。',
jobId: 56537,
jobPlaceList: []
}
],
jobTypeList: [
{
id: 803,
city: '会计',
jobId: 56537
},
{
id: 803,
city: '法学',
jobId: 56537
}
]
}
]
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
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
# 2.入参说明
# 主入参
| 参数 | 说明 | 类型 | 是否必填 | 默认值 |
|---|---|---|---|---|
| data | 原数据,详情见下 👇 | Object Array | 是 |
# data 中每一项数组
| 参数 | 说明 | 类型 | 是否必填 | 默认值 |
|---|---|---|---|---|
| parent | 上级对象,详情见下 👇 | Object | 是 | |
| child | 子级对象,详情见下 👇 | Object | 是 | |
| fieldName | 要生成子列表的键名 | String | 是 | children |
# parent 对象
| 参数 | 说明 | 类型 | 是否必填 | 默认值 |
|---|---|---|---|---|
| list | 上级数据 (父列表) list | Array | 是 | |
| id | 主键名 | String | 是 |
# child 对象
| 参数 | 说明 | 类型 | 是否必填 | 默认值 |
|---|---|---|---|---|
| list | 子级数据(子列表)list | Array | 是 | |
| pId | (父级)上级主键名 | String | 是 |
# 3.源码
import isArray from '@/base/isArray'
/**
* 匹配子列表,形成树状
* @param {Array} data
* @example
*data = [
{
parent: { list: data_1, id: 'id' },
child: { list: data_2, pId: 'parentGId' },
fieldName: '_1children'
}
]
其中parent是父列表对象,属性:list是父列表,id是列表里面每一项主键名 (必传)
其中child是子列表对象,属性:list是子列表,pId是列表里面每一项上级(父级)主键名 (必传)
fieldName是生成子列表的键名, 默认值是children
* @returns {{}}
*/
function matchSubListGroup(data = []) {
if (isArray(data)) {
data.forEach(v => {
const { parent, child, fieldName = 'children' } = v
if (isArray(parent.list) && isArray(child.list)) {
parent.list.forEach(o => {
if (!o[fieldName]?.length) o[fieldName] = []
child.list.forEach(k => {
if (k[child.pId] == o[parent.id]) {
o[fieldName].push(k)
}
})
})
}
})
}
return data[0]?.parent?.list || []
}
export default matchSubListGroup
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
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
上次更新: 2023/06/24, 19:35:48