【问题标题】:Is it possible to iterate through a conditional statement for the length property in a for loop?是否可以在 for 循环中遍历长度属性的条件语句?
【发布时间】:2018-10-15 18:18:41
【问题描述】:

感谢您抽出宝贵时间查看此内容。代码如下。

let questions = [];

let Question = function(question, answers, number) {
    this.question = question;
    this.answers = answers;
    this.number = number;
}

let question1 = new Question('Is Javascript the best programming language?', 
['No', 'Yes'], 1);

let question2 = new Question('Is Javascript a web development language?', 
['No', 'Yes'], 1);

let question3 = new Question(`What is Javascript's official server side 
application called?`, ['BootStrap', 'Python', 'Node.js'], 2);

questions = [question1, question2, question3];

Question.randomQuestion = function() {
    let randomNum = Math.floor(Math.random() * 3);
    let randomQuestion = questions[randomNum];

    return randomQuestion;
}

Question.display = function(randomQuestion) {
    console.log(randomQuestion.question);
    debugger;
        // Why does the conditional give me undefined?
        for (let i = 0; i < questions[i].answers[i].length; i++)
        console.log(`${randomQuestion.answers[i]}`);    
    }

我想要做的是通过每次迭代获取 answers 属性的长度,但我得到一个错误: answers 是未定义的。为什么它是未定义的,有没有更好的方法呢?

【问题讨论】:

  • 您将索引i 应用于问题数组和每个问题的answer 属性,这没有任何意义。
  • 您介意详细说明一下吗?
  • questions[i].answers[i] 表示“第 i 个问题的第 i 个答案”。这真的是您想要的吗?您不应该用一个索引遍历问题,然后遍历每个问题都有另一个索引?
  • 你的 for 循环应该像这样for (let i = 0; i &lt; randomQuestion.answers[i].length; i++)
  • mm,你说得对,他需要第二个 for 循环来遍历问题,然后遍历答案

标签: javascript arrays object for-loop iteration


【解决方案1】:

有很多错误。基本上,当您遍历两个数组时,您需要两个循环。 我用我的代码制作了这个 sn-p,希望对您有所帮助:

let questions = [];

let Question = function (question, answers, number) {
  this.question = question;
  this.answers = answers;
  this.number = number;
}

let question1 = new Question('Is Javascript the best programming language?',
  ['No', 'Yes'], 1);

let question2 = new Question('Is Javascript a web development language?',
  ['No', 'Yes'], 1);

let question3 = new Question(`What is Javascript's official server side 
application called?`, ['BootStrap', 'Python', 'Node.js'], 2);

questions = [question1, question2, question3];

const randomQuestion = function () {
  let randomNum = Math.floor(Math.random() * 3);
  let randomQuestion = questions[randomNum];
  return randomQuestion;
}

const display = function (randomQuestion) {
  const random_question = randomQuestion();
  console.log(random_question.question);
  //debugger;
  // Why does the conditional give me undefined?
  //dump all question and all answers
  for (let i = 0; i < questions.length; i++)
    for (let j = 0; j < questions[i].answers.length; j++)
      console.log(`${questions[i].answers[j]}`);
  //as an alternative :
  questions.forEach(e => {
    console.log(e.question);
    e.answers.forEach(a => {
      console.log(a)
    })
  })
}

display(randomQuestion);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-31
    • 2018-05-27
    • 2014-08-15
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 2013-10-15
    • 2011-09-05
    相关资源
    最近更新 更多