I have an array below.
For each "_id" in each object element in this array, there is an array called "form".
What I would like to get is the answerValue for each _id, where the field value in form is cState.
let array = [{
"_id" : "a1",
"user" : "John",
"form" : [
{
"question" : "question1",
"questionValue" : "Which state do you live in?",
"answers" : [
"answer1"
],
"answerValue" : [
"Arizona"
],
"field" : "cState",
},
{
"question" : "question2",
"questionValue" : "What is your nationality?",
"answers" : [
"answer2"
],
"answerValue" : [
"American"
],
"field" : "nationality",
},
"_id" : "a2",
"user" : "Mike",
"form" : [
{
"question" : "question1",
"questionValue" : "Which state do you live in?",
"answers" : [
"answer3"
],
"answerValue" : [
"Florida"
],
"field" : "cState",
},
{
"question" : "question2",
"questionValue" : "What is your nationality?",
"answers" : [
"answer2"
],
"answerValue" : [
"American"
],
"field" : "nationality",
},
}]
Expected Output
[{
"_id" : "a1",
"user" : "John",
"answerValue": "Arizona"
},
{
"_id" : "a2",
"user" : "Mike",
"answerValue": "Florida"
},
]
Here's what I tried:
let allDemographics
allDemographics = array.map((item) => {
return {
user: item.array._id,
nationality: item.array.nationality,
state: item.array.form,
}
})