Home > database >  How to compare data from two array.map
How to compare data from two array.map

Time:11-18

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: "[email protected]"
  },
  {
    answers: {
      bida01: "b",
      bida02: "b",
      bida03: "a",
    },
    category: "bida",
    firstName: "Jennifer",
    lastName: "Harley",
    school: "Somewhere Junior High",
    address: "Also somewhere",
    email: "[email protected]"
  }
]




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);

});
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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?

CodePudding user response:

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.

CodePudding user response:

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: "[email protected]"
  },
  {
    answers: {
      bida01: "b",
      bida02: "b",
      bida03: "a"
    },
    category: "bida",
    firstName: "Jennifer",
    lastName: "Harley",
    school: "Somewhere Junior High",
    address: "Also somewhere",
    email: "[email protected]"
  }
];

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())
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related