我想通过比较两个 JSON,基于公共(public)属性值创建两个 JSON 数组。
this.linkedParticipants =[
{
"id": 3,
"name": "Participant 2",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e999889b9d808a809988879ddba98f808a86c78a8684" rel="noreferrer noopener nofollow">[email protected]</a>"
},
{
"id": 2,
"name": "Participant 1",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d0d1c0f09141e140d1c13094c3d1b141e12531e1210" rel="noreferrer noopener nofollow">[email protected]</a>"
},
{
"id": 1,
"name": "Libin Varghese",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e02272c2720182f3c29262b3d2b0e28272d21602d2123" rel="noreferrer noopener nofollow">[email protected]</a>"
},
{
"id": 7,
"name": "Participant 5",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbcbdac9cfd2d8d2cbdad5cf8efbddd2d8d495d8d4d6" rel="noreferrer noopener nofollow">[email protected]</a>"
}
]
this.appointmentList = [
{
"id": 32,
"participant": {
"id": 1,
"name": "Libin Varghese",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b57727972754d7a697c737e687e5b7d72787435787476" rel="noreferrer noopener nofollow">[email protected]</a>"
}
},
{
"id": 33,
"participant": {
"id": 7,
"name": "Participant 5",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f2f3e2d2b363c362f3e312b6a1f39363c30713c3032" rel="noreferrer noopener nofollow">[email protected]</a>"
}
}
]
this.invitedList = [];
this.confirmedList = [];
let temp = {}
this.linkedParticipants.forEach((participant, i) => {
this.appointmentList.forEach((appointment, j) => {
if (appointment.participant.name.indexOf(participant.name)) {
temp = {
'participant': participant
}
this.invitedList.push(temp);
}
else {
this.confirmedList.push(appointment)
}
})
})
但是代码没有按预期工作,因为 this.invitedList 的两个值给出了重复的值。我认为我的比较条件有一些问题。
最佳答案
您可以通过 filter()
的不同组合来实现此目的:
var linkedParticipants =[
{
"id": 3,
"name": "Participant 2",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1e6e7f6c6a777d776e7f706a2c5e78777d71307d7173" rel="noreferrer noopener nofollow">[email protected]</a>"
},
{
"id": 2,
"name": "Participant 1",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="22524350564b414b52434c561362444b414d0c414d4f" rel="noreferrer noopener nofollow">[email protected]</a>"
},
{
"id": 1,
"name": "Libin Varghese",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4b07222922251d2a392c232e382e0b2d22282465282426" rel="noreferrer noopener nofollow">[email protected]</a>"
},
{
"id": 7,
"name": "Participant 5",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2c5c4d5e58454f455c4d4258196c4a454f43024f4341" rel="noreferrer noopener nofollow">[email protected]</a>"
}
];
var appointmentList = [
{
"id": 32,
"participant": {
"id": 1,
"name": "Libin Varghese",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82ceebe0ebecd4e3f0e5eae7f1e7c2e4ebe1edace1edef" rel="noreferrer noopener nofollow">[email protected]</a>"
}
},
{
"id": 33,
"participant": {
"id": 7,
"name": "Participant 5",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6818091a1c010b011809061c5d280e010b07460b0705" rel="noreferrer noopener nofollow">[email protected]</a>"
}
}
];
var confirmedList = appointmentList.filter((a) => {
return linkedParticipants.find((p) => p.id === a.participant.id);
});
var invitedList = linkedParticipants.filter((p) => {
return !appointmentList.find((a) => p.id === a.participant.id);
});
console.log(confirmedList)
console.log(invitedList)
关于javascript - 比较两个 JSON 并创建具有共同值的 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63014611/