在javascript中的第一个数组元素上未定义

时间:2016-06-07 09:06:16

标签: javascript arrays loops object undefined

我在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;
&#13;
&#13;

1 个答案:

答案 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 );

  }