const questions = [
{
"question": "What is the scientific name of a butterfly?",
"answers": ["Apis", "Coleoptera", "Formicidae", "Rhopalocera"],
"correctIndex": 3
},
{
"question": "How hot is the surface of the sun?",
"answers": ["1,233 K", "5,778 K", "12,130 K", "101,300 K"],
"correctIndex": 1
},
]
我正在尝试这种方式,但返回-1:
console.log(questions.findIndex(value => value.answers.indexOf() === 'Apis'));
例如,如果要获取indexOf'Apis',我得到-1。
我正在尝试将'CorrectIndex'值与答案数组值的索引进行比较,并返回其正确与否。
您只是以错误的方式使用了indexOf。修复起来很简单:
const questions = [
{
question: "What is the scientific name of a butterfly?",
answers: ["Apis", "Coleoptera", "Formicidae", "Rhopalocera"],
correctIndex: 3
},
{
question: "How hot is the surface of the sun?",
answers: ["1,233 K", "5,778 K", "12,130 K", "101,300 K"],
correctIndex: 1
}
]
questions.forEach(question => {
console.log(question.answers.indexOf('Apis'))
});
意识到indexOf是一个函数并接收一个字符串。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句