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

119
Vistas
How to compare data from two array.map

So i have two sets of data:

The first data is the answer key of an exam.

The second data is the answers from students whom are doing the exam.

From those two data, i tried to array.map them both.

const answerKey = [{
    id: "bida01",
    answerKey: "b",
    category: "bida",
    nomorSoal: "01",
    score: 20
  },
  {
    id: "bida02",
    answerKey: "b",
    category: "bida",
    nomorSoal: "02",
    score: 30
  }
]



const participants1 = [{
    answers: {
      bida01: "a",
      bida02: "b",
      bida03: "c",
    },
    category: "bida",
    firstName: "John",
    lastName: "Collins",
    school: "Something Junior High",
    address: "Somewhere",
    email: "blabla@bla.com"
  },
  {
    answers: {
      bida01: "b",
      bida02: "b",
      bida03: "a",
    },
    category: "bida",
    firstName: "Jennifer",
    lastName: "Harley",
    school: "Somewhere Junior High",
    address: "Also somewhere",
    email: "notsure@bla.com"
  }
]




const answerKeyData = answerKey.map((elem) => {
  console.log(elem.id, elem.answerKey, elem.score);
})

const participantData = participants1.map((elem, idx) => {
  console.log(elem.answer, elem.firstName, elem.middleName, elem.lastName, elem.school);

});

Now i can see from the result of those console.logs that i can get the values i want to compare. My purpose here is to compare student's answer to the answer keys, and then if they are a match:

return score += elem.score

But how do i access that elem.id, elem.answerKey, elem.score? Should i make a new object (lets say objectAnswerKey) with them? How can i do that?

The same with elem.firstName, elem.middleName, elem.lastName, elem.school, and elem.answer. Should i make a new object (lets say objectAnswer) and then compare objectAnswer and objectAnswerKey?

Or is there a more simple way?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use nested map/forEach/reduce array methods to compute scores by comparing question id and answerKey like the one below,

const participantsWithScore = participants1.map(student => {
    const answers = student.answers;
    const questions = Object.keys(answers);
    let score = 0;
    questions.forEach(question => {
        const answer = answers[question];
        const correctAnswer = answerKey.find(key => key.id === question);
        if(correctAnswer && correctAnswer.answerKey === answer) {
            score += correctAnswer.score;
        }
    });
    student.score = score;
    return student;
});
console.log(participantsWithScore);

In the console, you can see all the participants having the computed score property.

about 4 years ago · Juan Pablo Isaza Denunciar

0

const answerKey = [
  {
    id: "bida01",
    answerKey: "b",
    category: "bida",
    nomorSoal: "01",
    score: 20
  },
  {
    id: "bida02",
    answerKey: "b",
    category: "bida",
    nomorSoal: "02",
    score: 30
  }
];

const participants1 = [
  {
    answers: {
      bida01: "a",
      bida02: "b",
      bida03: "c"
    },
    category: "bida",
    firstName: "John",
    lastName: "Collins",
    school: "Something Junior High",
    address: "Somewhere",
    email: "blabla@bla.com"
  },
  {
    answers: {
      bida01: "b",
      bida02: "b",
      bida03: "a"
    },
    category: "bida",
    firstName: "Jennifer",
    lastName: "Harley",
    school: "Somewhere Junior High",
    address: "Also somewhere",
    email: "notsure@bla.com"
  }
];

const handleCalculate = () => {
    const studensScore = [];
    participants1.forEach((student) => {
      let score = 0;
      answerKey.forEach((answerKeyItem) => {
        const userAnswer = student.answers[answerKeyItem.id];
        if (userAnswer === answerKeyItem.answerKey) {
          score += answerKeyItem.score;
        }
      });
      const studentData = {
        fullname: student.firstName + " " + student.lastName,
        score: score
      };
      studensScore.push(studentData);
    });
    return studensScore;
  };
  
  console.log(handleCalculate())

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