尤其是我想访问答案的数值,以便稍后对其求和。
以前尝试过的长度,不适用于对象。
这是我的数据:
const qData = [
{
id: 0,
question: "question 1",
answers: [
{ value: 1, text: "rafael" },
{ value: 2, text: "dontaelo" },
{ value: 3, text: "leonardo" },
{ value: 4, text: "michelangelo" }
]
},
{
id: 1,
question: "question 2",
answers: [
{ value: 1, text: "rafael" },
{ value: 2, text: "dontaelo" },
{ value: 3, text: "leonardo" },
{ value: 4, text: "michelangelo" }
]
}
];
export default qData;
我试图像这样对答案值求和:
handleShowScore = () => {
var i, newScore;
var a = qData.answers;
for (i = 0; i < a.length; i++) {
newScore = newScore + a[i].value;
}
}
答案 0 :(得分:0)
如果我正确理解了您的问题,以下示例将满足您的要求
// Loop through all questions. Each question is stored in "q".
qData.forEach((q) => {
console.log('qData:', q);
// Make an array of all answer values
const answerValues = q.answers.map((a) => {
return a.value;
});
console.log('answerValues:', answerValues);
// Sum all answer values together
const totalValues = answerValues.reduce((a, b) => a + b, 0)
console.log('totalValues: ', totalValues);
});
如果您想获得特定ID的总和
// Find specific ID.
const question = qData.find((q) => q.id === 0);
// Make an array of all answer values
const answerValues = question.answers.map((a) => {
return a.value;
});
console.log('answerValues:', answerValues);
// Sum all answer values together
const totalValues = answerValues.reduce((a, b) => a + b, 0)
console.log('totalValues: ', totalValues);
答案 1 :(得分:0)
letter_opener
是一个数组,没有属性qData
,所以answers
将不起作用并返回qData.answers
。
您首先必须遍历问题,然后在每个问题中必须遍历答案:
undefined
如果使用flatMap
,则可以减少循环次数。将答案汇总成一个大数组。
const qData = [{id:0,question:"question 1",answers:[{value:1,text:"rafael"},{value:2,text:"dontaelo"},{value:3,text:"leonardo"},{value:4,text:"michelangelo"}]},{id:1,question:"question 2",answers:[{value:1,text:"rafael"},{value:2,text:"dontaelo"},{value:3,text:"leonardo"},{value:4,text:"michelangelo"}]}];
let sum = 0;
for (const question of qData) {
for (const answer of question.answers) {
sum += answer.value;
}
}
console.log(sum);
除了使用for循环求和之外,还可以使用reduce
,它会在数组上进行迭代以将其减少为单个值。
const qData = [{id:0,question:"question 1",answers:[{value:1,text:"rafael"},{value:2,text:"dontaelo"},{value:3,text:"leonardo"},{value:4,text:"michelangelo"}]},{id:1,question:"question 2",answers:[{value:1,text:"rafael"},{value:2,text:"dontaelo"},{value:3,text:"leonardo"},{value:4,text:"michelangelo"}]}];
const answers = qData.flatMap(question => question.answers);
let sum = 0;
for (const answer of answers) {
sum += answer.value;
}
console.log(sum);
答案 2 :(得分:0)
有点测验,对吧?假设我的答案是:
const myAnswers = [{question: 'question 1', myAnswer:'rafael'},{question: 'question 2', myAnswer:'dontaelo'}]
我从第一个问题得1分,从第二个问题得2分。我们需要经历2个循环:1找到相应的问题,1找到相应的点数:
const myPoints = myAnswers.map(answer => qData.find(question => question.question === answer.question).answers.find(possibleAnswer => possibleAnswer.text === answer.myAnswer).value)
那给了我[1,2]。现在我们需要用reduce求和:
const reducer = (accumulator, currentValue) => accumulator + currentValue;
const myScore = myPoints.reduce(reducer, 0);
我有3分;-)