1 回答
TA贡献1835条经验 获得超7个赞
再见,你可以按照这个例子:
const obj = {
question: "What is your dog name?",
answer: "Browny"
};
const array = [
{
question: "What is your name?",
answer: "Diana"
},
{
question: "What is your dog name?",
answer: "Browny"
}
];
let correct_answer = array.filter(el => el.question === obj.question)[0];
if (correct_answer.answer === obj.answer) console.log("correct answer");
else console.log("wrong answer");
所以你的代码变成:
let correct_answer = array.filter(arr => arr.question === obj.question);
if (correct_answer.length === 0) response.status(409).json({message: 'Wrong question chosen'})
else {
if (correct_answer[0].answer === obj.answer) response.status(200);
else response.status(409).json({message: 'Please type the right answer'});
}
添加回答
举报