如何获取对象内数组元素的索引?
How can I get the index of array element inside object?
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
},
]
我正在尝试这种方式,但是 return -1:
console.log(questions.findIndex(value => value.answers.indexOf() === 'Apis'));
例如,如果想获得 indexOf 'Apis',我得到 -1。
我正在尝试比较 'CorrectIndex' 值与答案数组值的索引以及 return 是否正确。
你为什么要比较value.answers.indexOf()
正确的语法应该是
console.log(questions.findIndex(value => value.answers.indexOf('element')));
您只是错误地使用了 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 是一个函数并接收一个字符串。
如果我没理解错的话,你想获取这个对象的answers
数组中元素的索引。
试试这个:
console.log(questions.map(value => value.answers.indexOf('Apis')));
这将为您提供一个值为“Apis”的索引数组。
如果同一个数组中有重复值,可以这样做:
console.log(questions.map(value => [value.answers.indexOf('Apis')]));
这将存储一个数组的数组,您可以在其中获取每个对象的“Apis”的索引。
I'm trying to compare 'CorrectIndex' value with index of answers array
value and return if its correct or not.
试试这个
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
},
]
console.log(questions.map(value => value.answers.indexOf('Apis') === value.correctIndex));
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
},
]
我正在尝试这种方式,但是 return -1:
console.log(questions.findIndex(value => value.answers.indexOf() === 'Apis'));
例如,如果想获得 indexOf 'Apis',我得到 -1。
我正在尝试比较 'CorrectIndex' 值与答案数组值的索引以及 return 是否正确。
你为什么要比较value.answers.indexOf()
正确的语法应该是
console.log(questions.findIndex(value => value.answers.indexOf('element')));
您只是错误地使用了 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 是一个函数并接收一个字符串。
如果我没理解错的话,你想获取这个对象的answers
数组中元素的索引。
试试这个:
console.log(questions.map(value => value.answers.indexOf('Apis')));
这将为您提供一个值为“Apis”的索引数组。
如果同一个数组中有重复值,可以这样做:
console.log(questions.map(value => [value.answers.indexOf('Apis')]));
这将存储一个数组的数组,您可以在其中获取每个对象的“Apis”的索引。
I'm trying to compare 'CorrectIndex' value with index of answers array value and return if its correct or not.
试试这个
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
},
]
console.log(questions.map(value => value.answers.indexOf('Apis') === value.correctIndex));