1 回答

TA貢獻(xiàn)1835條經(jīng)驗 獲得超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'});
}
添加回答
舉報