所以我有兩組資料:
第一個資料是考試的答案鍵。
第二個資料是參加考試的學生的答案。
從這兩個資料中,我嘗試對它們進行 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: "[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);
});
現在我可以從那些 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 嗎?
或者有更簡單的方法嗎?
uj5u.com熱心網友回復:
您可以使用嵌套的 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);
在控制臺中,您可以看到所有具有計算分數屬性的參與者。
uj5u.com熱心網友回復:
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())
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/361231.html
標籤:javascript 数组 目的 相比
上一篇:JQ根據鍵值獲取唯一的物件和陣列
