我遇到了一個問題。
所以我正在制作這個學生評分系統。它的作業方式是系統應該能夠將答案密鑰與學生的答案相匹配。答案鍵是這樣存盤的:
const answerkeys = {math01: a, math02: b, math03 : d, ...}
還有一個評分系統,其中 math01 值 5 分,數學 02 值 10 分,等等。出于混淆,我將資料保存在另一組資料集中,如下所示:
const scoring = {math01: 5, math02: 10, math03: 5, ...}
然后學生的答案是這樣存盤的:
const answers = {0: {math01: a, math02: c, ... }
1: {math01: b, math02: a, ... }
2: {math01: a, math02: c, ... }
...
}
起初,從邏輯上講,我認為我可以迭代答案,然后將單個結果與 answerkeys 匹配,然后回傳的(匹配項)將與 answerkeyandscore 相乘。
所以我嘗試這樣做:
const studentAnswers = answers.map((elem) => {return elem});
studentAnswer.filter(answer => answerkeys.includes(answer));
但它給了我
“result.js:82 未捕獲的型別錯誤:無法讀取未定義的屬性(讀取‘過濾器’)”
如何比較“answerkeys”和“answers”,然后使用“scoring”計算分數?
uj5u.com熱心網友回復:
map該Object物件沒有本機,但我建議這會起作用:
let allStudentsScores = {}
for (var idx in answers) {
let studentAnswer = answers[idx]
let studentScore = 0
for(var key in studentAnswer) {
if (studentAnswer[key]==answerkeys[key]) {
studentScore = scoring[key]
}
}
allStudentsScores[idx] = studentScore
}
該allStudentsScores變數包含每個學生的分數。其中idx <0, 1, 2, ...>代表每個學生。
希望回答你的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/361577.html
標籤:javascript 数组 目的 相比
上一篇:查找物件陣列的最小值
