在我正在工作的一个项目中,我需要合并2个对象数组,结果一个新的对象数组包含合并的键/值,其中两个id匹配。
举个例子,我创建了下面的代码片段,但对我想要实现的目标并不完全正确。在代码片段
之后的更多细节
const users = [{
id: 'Ae7uWu7LjwoEgVqzFU5xc',
firstName: 'Carl',
lastName: 'Jones'
},
{
id: 't2wzj8dh4r-qw1_SW-IOE',
firstName: 'Chloe',
lastName: 'Kearney'
},
{
id: '50Zwvw37OejbQBG7csZWJ',
firstName: 'Gemma',
lastName: 'Sloan'
},
{
id: 'NpcXdEKqfzhVCZOJ1dKuw',
firstName: 'Dario',
lastName: 'Angelini'
},
{
id: 'e95ZG9IfV442HdJp-CaBL',
firstName: 'Mindy',
lastName: 'Schreiber'
},
{
id: 'eAMv8AbynYRkdBPE5Scm2',
firstName: 'Xdax',
lastName: 'Rufq'
},
{
id: 'egMXnFvoMM7f4in3Se4Ui',
firstName: 'Mtajx',
lastName: 'Plde'
},
{
id: '6kbPT-HC5-szACuJ85I6r',
firstName: 'Zsofi',
lastName: 'Toke'
}
]
const comments = [{
content: 'Patient follow up call scheduled for 11Nov2021 at 8am',
stage: 'AT_CALLCENTER',
userId: 't2wzj8dh4r-qw1_SW-IOE',
createdAt: '2021-10-29T11:41:11.780Z'
},
{
content: 'Patient confirmed GP referral on [date]',
stage: 'AT_CALLCENTER',
userId: 't2wzj8dh4r-qw1_SW-IOE',
createdAt: '2021-10-29T11:41:42.237Z'
},
{
content: 'Candidate called on [date] and visit scheduled for [date] ',
stage: 'AT_SITE',
userId: 't2wzj8dh4r-qw1_SW-IOE',
createdAt: '2021-10-29T11:43:42.140Z'
},
{
content: 'Candidate test result was positive for Pompe disease on [date]',
stage: 'AT_SITE',
userId: 't2wzj8dh4r-qw1_SW-IOE',
createdAt: '2021-10-29T11:45:21.716Z'
}
]
const result = users.map(t1 => ({
...t1,
content: comments.filter(t2 => t2.userId === t1.id).map(t1 => t1.content),
created: comments.filter(t2 => t2.userId === t1.id).map(t1 => t1.createdAt),
}));
console.log(result)
我能从那个尝试中得到的结果是一个对象数组但是注释被表示为用户对象本身中的数组对于createdAt
这是我现在得到的一个例子
[{
"id": "t2wzj8dh4r-qw1_SW-IOE",
"firstName": "Chloe",
"lastName": "Kearney",
"content": [
"Patient follow up call scheduled for 11Nov2021 at 8am",
"Patient confirmed GP referral on [date]",
"Candidate called on [date] and visit scheduled for [date] ",
"Candidate test result was positive for Pompe disease on [date]"
],
"created": [
"2021-10-29T11:41:11.780Z",
"2021-10-29T11:41:42.237Z",
"2021-10-29T11:43:42.140Z",
"2021-10-29T11:45:21.716Z"
]
},]
我想要的是绕道而行,我的意思是我想要注释包含当comments.userId
=users.id
时,用户的firstName
和lastName
我想要达到的目标的例子
考虑
下面的两个对象comments = [{
content: 'Patient follow up call scheduled for 11Nov2021 at 8am',
stage: 'AT_CALLCENTER',
userId: 't2wzj8dh4r-qw1_SW-IOE',
createdAt: '2021-10-29T11:41:11.780Z'
},
{
content: 'Patient confirmed GP referral on [date]',
stage: 'AT_CALLCENTER',
userId: 't2wzj8dh4r-qw1_SW-IOE',
createdAt: '2021-10-29T11:41:42.237Z'
}, { ...some other comments...}]
users = [
{
id: 't2wzj8dh4r-qw1_SW-IOE',
firstName: 'Chloe',
lastName: 'Kearney'
},
{ ...some other users... }]
两个对象合并后的结果如下
res = [
{
firstName: 'Chloe',
lastName: 'Kearney',
content: 'Patient follow up call scheduled for 11Nov2021 at 8am',
stage: 'AT_CALLCENTER',
userId: 't2wzj8dh4r-qw1_SW-IOE',
createdAt: '2021-10-29T11:41:11.780Z'
},
{
firstName: 'Chloe',
lastName: 'Kearney',
content: 'Patient confirmed GP referral on [date]',
stage: 'AT_CALLCENTER',
userId: 't2wzj8dh4r-qw1_SW-IOE',
createdAt: '2021-10-29T11:41:42.237Z'
}
]
如上所述,我在有id匹配的地方添加了用户的姓和名以及注释,因此合并后它们在一起。
我正在寻找一种好的方法来做到这一点
const comments = [{
content: 'Patient follow up call scheduled for 11Nov2021 at 8am',
stage: 'AT_CALLCENTER',
userId: 't2wzj8dh4r-qw1_SW-IOE',
createdAt: '2021-10-29T11:41:11.780Z'
},{
content: 'Patient confirmed GP referral on [date]',
stage: 'AT_CALLCENTER',
userId: 't2wzj8dh4r-qw1_SW-IOE',
createdAt: '2021-10-29T11:41:42.237Z'
}];
const users = [{
id: 't2wzj8dh4r-qw1_SW-IOE',
firstName: 'Chloe',
lastName: 'Kearney'
}];
console.log(
comments.map(comment => {
const {id, ...user} = users.find(({id}) => id===comment.userId);
return ({...user, ...comment});
}));