I have 2 array of objects that look like this
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",
}
]
I need to compare the pollId from pollAnsers with _id in polls to add the matching answers to the corresponding pollQuestions in the following way
"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",
}
]
}
]
I have been trying all possibilities like using map, filter, for loops etc but haven't been able to get the result, I am fairly new to this please help ! thanks