我在JavaScript中尝试一个简单的测验应用程序。我有一系列问题对象,当我遍历数组并打印出问题时,它在第一个问题上显示未定义但第二个问题是将问题打印到console.log。任何人都知道这里发生了什么。
感谢。
var questions =
[
{
questions: "What color is of milk?",
choices: ["White", "Blue", "Brown", "Red"],
answer: 0
},
{
question: "What is Tallest Building in the world?",
choices: ["Eifle Tower","Burg Khalifa", "Shenghai Tower" ],
answer: 1
}
];
for ( var i = 0; i < questions.length; i++ ) {
question = questions[i].question;
choices = questions[i].choices;
answer = questions[i].answer;
console.log ( question );
console.log ( choices );
console.log ( answer );
}
&#13;
答案 0 :(得分:2)
不应该questions: "What color is of milk?",
question: "What color is of milk?",
这很好用。
var questions =
[
{
question: "What color is of milk?",
choices: ["White", "Blue", "Brown", "Red"],
answer: 0
},
{
question: "What is Tallest Building in the world?",
choices: ["Eifle Tower","Burg Khalifa", "Shenghai Tower" ],
answer: 1
}
];
for ( var i = 0; i < questions.length; i++ ) {
question = questions[i].question;
choices = questions[i].choices;
answer = questions[i].answer;
console.log ( question );
console.log ( choices );
console.log ( answer );
}