Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

246
Vistas
Get the value of specific fields in an array based on a condition

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,
    }
  })
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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)

about 4 years ago · Juan Pablo Isaza Denunciar

0

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 })   
 )
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda