Javascript For循环不遍历每个语句
我是一个初学者,也有一段时间对Ruby感兴趣,所以我可能错过了一些小东西 但我很好奇为什么console.log(a删除[I].win);[i]的每次迭代是否都被跳过?我发现当计数器到达该点时,它已经到了2,因此它返回未定义,因为它已经超过了数组的末尾 奇怪的是,如果我把它前面的语句中的分号改成逗号,它就会起作用。因此,出于某种原因,迭代器只对第一条语句起作用Javascript For循环不遍历每个语句,javascript,object,for-loop,Javascript,Object,For Loop,我是一个初学者,也有一段时间对Ruby感兴趣,所以我可能错过了一些小东西 但我很好奇为什么console.log(a删除[I].win);[i]的每次迭代是否都被跳过?我发现当计数器到达该点时,它已经到了2,因此它返回未定义,因为它已经超过了数组的末尾 奇怪的是,如果我把它前面的语句中的分号改成逗号,它就会起作用。因此,出于某种原因,迭代器只对第一条语句起作用 var tim = { name : "Tim", height : 71, sport : "soccer",
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
*/
var-tim={
姓名:“蒂姆”,
身高:71,
运动:“足球”,
引语:“老虎蒂米!”
}
var anne={
姓名:“安妮”,
身高:66,
运动:“尊巴”,
引语:“安妮要在你身上跳舞!”
}
//函数向每个运动员对象添加“win”属性和默认值
函数addWinners(a删除){
对于(变量i=0;i“无法读取未定义的属性'win'”
期望输出:
=>蒂姆赢得了足球比赛
=>Anne won zumba
*/
在使用{…}
执行for
循环后,没有启动代码块。这样,只有直接在循环之后的语句才会在循环内部执行。之后的所有操作都将在循环完成且i
等于2后执行。Edit:i
在调用console.log()
的地方实际上不应该再存在了,因为它不在范围之内——它已在for
语句中声明
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
*/
请像这样尝试:
for(var i = 0; i < atheletes.length; i++) {
atheletes[i].win = (atheletes[i].name + " won " +
atheletes[i].sport);
console.log(atheletes[i].win);
}
for(变量i=0;i
for循环周围没有大括号,因此可以有效地编写
for(var i = 0; i < atheletes.length; i++) {
atheletes[i].win = (atheletes[i].name + " won " + atheletes[i].sport);
}
console.log(atheletes[i].win);
for(变量i=0;i
使用大括号并将这两个语句放在里面。应该是
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);
}
}
函数addWinners(a删除){
对于(变量i=0;i
你错过了{}for循环。我想是JavaScript'y!您好,谢谢@TimoSta。变量
i
不应该存在于循环范围之外,因为它是在仅供参考的for
语句中初始化的。有没有删除文本的方法?如果它是错误的,但我想保留它,以便读者理解答案的注释?
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)