【问题标题】:For-loop not iterating over each of the statementsFor-loop 不迭代每个语句
【发布时间】:2015-05-19 01:04:40
【问题描述】:

我是一个初学者,并且对 Ruby 也有一定的了解,所以我可能在这里遗漏了一些小东西。

但我很好奇为什么 console.log(atheletes[i].win); 会在 [i] 的每次迭代中被跳过?我发现当计数器到达那个点时,它已经是 2,所以它返回 undefined,因为它超过了数组的末尾。

奇怪的是,如果我将分号从它之前的语句更改为逗号,它就可以工作。所以由于某种原因,迭代器只适用于第一条语句。

var tim = {
    name : "Tim",
    height : 71,
    sport : "soccer",
    quote : "Timmy the Tiger!"
}

var anne = {
    name : "Anne",
    height : 66,
    sport : "zumba",
    quote : "Anne Gonna Dance All Over Ya!"
}

//function to add a "win" property and default value to each athlete object
function addWinners(atheletes){ 
    for(var i = 0; i < atheletes.length; i++)
        atheletes[i].win = (atheletes[i].name + " won " +     
        atheletes[i].sport);

        console.log(atheletes[i].win);
}

fridays_contestants=[tim,anne]
addWinners(fridays_contestants)


/*
Current output:
=>"Cannot read property 'win' of undefined"

Desired output:
=>Tim won soccer
=>Anne won zumba
*/

【问题讨论】:

    标签: javascript object for-loop


    【解决方案1】:

    在您的for 循环之后,您没有使用{ ... } 启动代码块。这样,只有循环之后的语句才会在循环内部执行。之后的所有内容都将在循环已经完成并且i 等于 2 后执行。编辑:i 实际上在您调用console.log() 时应该不再存在,因为它超出了范围 - 它已在for 语句中声明。

    试试这样吧:

    for(var i = 0; i < atheletes.length; i++) {
        atheletes[i].win = (atheletes[i].name + " won " +     
            atheletes[i].sport);
    
        console.log(atheletes[i].win);
    }
    

    【讨论】:

    • 我认为这是 JavaScript 的东西!好眼力,感谢@TimoSta。
    • 变量i不应存在于循环范围之外,因为它是在for语句中初始化的,仅供参考。
    • 有什么方法可以删除文本吗?万一它是错误的,但我想保留它以使答案的 cmets 对读者有意义?
    【解决方案2】:

    你的 for 循环没有大括号,所以你已经有效地编写了

    for(var i = 0; i < atheletes.length; i++) {
        atheletes[i].win = (atheletes[i].name + " won " + atheletes[i].sport);
    }
    console.log(atheletes[i].win);
    

    使用大括号并将两个语句放在里面。

    【讨论】:

      【解决方案3】:

      应该是

      function addWinners(atheletes){ 
      for(var i = 0; i < atheletes.length; i++)
          {
          atheletes[i].win = (atheletes[i].name + " won " +     
          atheletes[i].sport);
      
          console.log(atheletes[i].win);
      }
      }
      

      您缺少 {} for 循环。

      【讨论】:

        【解决方案4】:
          var tim = {
              name : "Tim",
              height : 71,
              sport : "soccer",
              quote : "Timmy the Tiger!"
          }
        
          var anne = {
              name : "Anne",
              height : 66,
              sport : "zumba",
              quote : "Anne Gonna Dance All Over Ya!"
          }
        
          //function to add a "win" property and default value to each athlete object
          function addWinners(atheletes){ 
              for(var key in tim){
                // var atheletes= (atheletes[i].name + " won " + atheletes[i].sport);
                if (tim.hasOwnProperty(key)) {
                    alert(key + " -> " + tim[key]);
                  }
                  console.log(atheletes);
          }
        }
          fridays_contestants=[tim,anne]
          addWinners(fridays_contestants)
        

        【讨论】:

          猜你喜欢
          • 2016-09-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-07-05
          • 1970-01-01
          • 1970-01-01
          • 2013-09-02
          • 2018-04-07
          相关资源
          最近更新 更多