Home > Software design >  Get the value of specific fields in an array based on a condition
Get the value of specific fields in an array based on a condition

Time:05-27

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,
    }
  })

CodePudding user response:

Try it :

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",
        },
    ]
  }
]
let allDemographics = array.map((item) => {
  let fieldCstate = item.form.find(form => form.field === "cState")
  return {  
    _id: item._id,
    user: item.user,
    answerValue: fieldCstate.answerValue[0],
  }
})
console.log(allDemographics)

CodePudding user response:

This one I like because you filter before you map. If you had a large array this might give you better performance:

let arr = array.filter(x => x.form.every(y => y.field === "cState"));
const data = arr.map((x) => ({ user: x.user, id: x._id, 
         answer:x.form[0].answerValue }));
    
console.log(data[0].answer)

Or a one liner:

let arr = array.filter(x => x.form.every(y => y.field === "cState"))
 .map(x => ({ user: x.user, id: x._id, answer:x.form[0].answerValue })   
 )
  • Related