gpt4 book ai didi

javascript - 比较两个对象数组并将具有匹配值的对象添加到第一个对象数组

转载 作者:太空宇宙 更新时间:2023-11-04 03:02:27 26 4
gpt4 key购买 nike

我有 2 个类似这样的对象数组

var pollAnswers = [
{
"_id": "5b58afa0c767e12c9869e540",
"pollId": "5b58afa0c767e12c9869e53f",
"option": "Google",
},
{
"_id": "5b58afa0c767e12c9869e541",
"pollId": "5b58afa0c767e12c9869e53f",
"option": "The Jetsons",
},
{
"_id": "5b58afa0c767e12c9869e542",
"pollId": "5b58afa0c767e12c9869e53f",
"option": "Family Guy",
},
{
"_id": "5b593b195c420e28089daf9d",
"pollId": "5b593b195c420e28089daf9c",
"option": "Yes. Through loyalty programmes.",
},
{
"_id": "5b593b195c420e28089daf9e",
"pollId": "5b593b195c420e28089daf9c",
"option": "What Hunger Crisis?",
},
{
"_id": "5b5953d775c4401e7052127c",
"pollId": "5b5953d775c4401e7052127b",
"option": "Yes, absolutely",
},
{
"_id": "5b5953d775c4401e7052127d",
"pollId": "5b5953d775c4401e7052127b",
"option": "No, absolutely not",
}
]

var polls = [
{
"_id": "5b58afa0c767e12c9869e53f",
"pollName": "Consumers in 2070 (How about now?)",
"pollQuestion": "Which animated series will consumers in 2070 resemble the most?",
},
{
"_id": "5b593b195c420e28089daf9c",
"pollName": "World Hunger",
"pollQuestion": "Can Internet-based services solve the Hunger Crisis?",
},
{
"_id": "5b5953d775c4401e7052127b",
"pollName": "Make things Work Again",
"pollQuestion": "Make things Work",
}
]

我需要将 pollAnsers 中的 pollId 与 polls 中的 _id 进行比较,以通过以下方式将匹配的答案添加到相应的 pollQuestions

"polls": [
{
"_id": "5b58afa0c767e12c9869e53f",
"pollName": "Consumers in 2070 (How about now?)",
"pollQuestion": "Which animated series will consumers in 2070 resemble the most?",
"answersList": [
{
"_id": "5b58afa0c767e12c9869e540",
"pollId": "5b58afa0c767e12c9869e53f",
"option": "Google",
},
{
"_id": "5b58afa0c767e12c9869e541",
"pollId": "5b58afa0c767e12c9869e53f",
"option": "The Jetsons",
},
{
"_id": "5b58afa0c767e12c9869e542",
"pollId": "5b58afa0c767e12c9869e53f",
"option": "Family Guy",
},
]
},
{
"_id": "5b593b195c420e28089daf9c",
"pollName": "World Hunger",
"pollQuestion": "Can Internet-based services solve the Hunger Crisis?",
"answersList": [
{
"_id": "5b593b195c420e28089daf9d",
"pollId": "5b593b195c420e28089daf9c",
"option": "Yes. Through loyalty programmes.",
},
{
"_id": "5b593b195c420e28089daf9e",
"pollId": "5b593b195c420e28089daf9c",
"option": "What Hunger Crisis?",
}
]
},
{
"_id": "5b5953d775c4401e7052127b",
"pollName": "Make things Work Again",
"pollQuestion": "Make things Work",
"answersList": [
{
"_id": "5b5953d775c4401e7052127c",
"pollId": "5b5953d775c4401e7052127b",
"option": "Yes, absolutely",
},
{
"_id": "5b5953d775c4401e7052127d",
"pollId": "5b5953d775c4401e7052127b",
"option": "No, absolutely not",
}
]
}
]

我一直在尝试所有可能性,例如使用 map 、过滤器、for 循环等,但无法获得结果,我对此还很陌生,请帮忙!谢谢

最佳答案

您可以通过简单易懂的代码使用两个 forEach() 循环:

var pollAnswers = [
{
"_id": "5b58afa0c767e12c9869e540",
"pollId": "5b58afa0c767e12c9869e53f",
"option": "Google",
},
{
"_id": "5b58afa0c767e12c9869e541",
"pollId": "5b58afa0c767e12c9869e53f",
"option": "The Jetsons",
},
{
"_id": "5b58afa0c767e12c9869e542",
"pollId": "5b58afa0c767e12c9869e53f",
"option": "Family Guy",
},
{
"_id": "5b593b195c420e28089daf9d",
"pollId": "5b593b195c420e28089daf9c",
"option": "Yes. Through loyalty programmes.",
},
{
"_id": "5b593b195c420e28089daf9e",
"pollId": "5b593b195c420e28089daf9c",
"option": "What Hunger Crisis?",
},
{
"_id": "5b5953d775c4401e7052127c",
"pollId": "5b5953d775c4401e7052127b",
"option": "Yes, absolutely",
},
{
"_id": "5b5953d775c4401e7052127d",
"pollId": "5b5953d775c4401e7052127b",
"option": "No, absolutely not",
}
]

var polls = [
{
"_id": "5b58afa0c767e12c9869e53f",
"pollName": "Consumers in 2070 (How about now?)",
"pollQuestion": "Which animated series will consumers in 2070 resemble the most?",
},
{
"_id": "5b593b195c420e28089daf9c",
"pollName": "World Hunger",
"pollQuestion": "Can Internet-based services solve the Hunger Crisis?",
},
{
"_id": "5b5953d775c4401e7052127b",
"pollName": "Make things Work Again",
"pollQuestion": "Make things Work",
}
];

polls.forEach((poll) => {
poll.answerList = [];
pollAnswers.forEach((pollAnswer) => {
if(pollAnswer.pollId === poll._id){
poll.answerList.push(pollAnswer);
}
});
});
console.log(polls);

关于javascript - 比较两个对象数组并将具有匹配值的对象添加到第一个对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51554078/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com