【问题标题】:How to compare data from two array.map如何比较来自两个array.map的数据
【发布时间】:2021-12-28 14:58:30
【问题描述】:

所以我有两组数据:

第一个数据是考试的答案。

第二个数据是参加考试的学生的答案。

根据这两个数据,我尝试对它们进行array.map。

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

});

现在我可以从这些 console.logs 的结果中看到我可以得到我想要比较的值。我在这里的目的是将学生的答案与答案键进行比较,然后如果它们匹配:

return score += elem.score

但是我如何访问 elem.id、elem.answerKey、elem.score?我应该用它们制作一个新对象(比如说objectAnswerKey)吗?我该怎么做?

与 elem.firstName、elem.middleName、elem.lastName、elem.school 和 elem.answer 相同。我应该创建一个新对象(比如说 objectAnswer)然后比较 objectAnswer 和 objectAnswerKey?

或者有更简单的方法吗?

【问题讨论】:

  • 我给你做了一个 sn-p 。请将其设为minimal reproducible example
  • 你可以做一个嵌套循环。首先,遍历参与者,然后遍历他们的答案,然后遍历问题,并检查参与者的答案是否等于您从问题数组中获得的正确答案。如果是,则将分数添加给用户。

标签: javascript arrays object compare


【解决方案1】:

您可以使用嵌套的 map/forEach/reduce 数组方法通过比较问题 id 和 answerKey 来计算分数,如下所示,

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

在控制台中,您可以看到所有具有计算得分属性的参与者。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2018-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多