Javascript:对嵌套 for 循环的工作方式感到困惑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6237692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: confused about how nested for loops work
提问by Stephen Young
Why do nested for loops
work in the way that they do in the following example:
为什么嵌套for loops
以下面示例中的方式工作:
var times = [
["04/11/10", "86kg"],
["05/12/11", "90kg"],
["06/12/11", "89kg"]
];
for (var i = 0; i < times.length; i++) {
var newTimes = [];
for(var x = 0; x < times[i].length; x++) {
newTimes.push(times[i][x]);
console.log(newTimes);
}
}
In this example I would have thought console.log would give me the following output:
在这个例子中,我会认为 console.log 会给我以下输出:
["04/11/10"]
["86kg"]
["05/12/11"]
["90kg"]
["06/12/11"]
["89kg"]
However, I actually get this:
但是,我实际上得到了这个:
["04/11/10"]
["04/11/10", "86kg"]
["05/12/11"]
["05/12/11", "90kg"]
["06/12/11"]
["06/12/11", "89kg"]
Is anyone able to help me understand this?
有没有人能够帮助我理解这一点?
EDIT:
编辑:
Thanks for all your responses!
感谢您的所有回复!
回答by Niklas
You are redefining newTimes
on every single loop and you are outputting to the console on each column push.
您正在重新定义newTimes
每个循环,并在每个列推送时输出到控制台。
var times = [
["04/11/10", "86kg"],
["05/12/11", "90kg"],
["06/12/11", "89kg"]
];
var newTimes = [];
for (var i = 0; i < times.length; i++) {
for(var x = 0; x < times[i].length; x++) {
newTimes.push(times[i][x]);
}
}
console.log(newTimes);
Returns: ["04/11/10", "86kg", "05/12/11", "90kg", "06/12/11", "89kg"]
http://jsfiddle.net/niklasvh/SuEdt/
返回:http: ["04/11/10", "86kg", "05/12/11", "90kg", "06/12/11", "89kg"]
//jsfiddle.net/niklasvh/SuEdt/
回答by scrypter
// remember that the increment of the counter variable
// is always executed after each run of a loop
for (var i = 0; i < n; i++) {
// some statement(s) to do something..
// initializes child-loop counter in the first run of the parent-loop
// resets child-loop counter in all following runs of the parent-loop
// while i is greater than 0 and lower than n
for (var j = 0; j < p; j++) {
// some statement(s) to do something..
// initializes grandchild-loop counter in the first run of the child-loop
// resets grandchild-loop counter in all following runs of the child-loop
// while j is greater than 0 and lower than p
for (var k = 0; k < q; k++) {
// some statement(s) to do something..
// or add more internal loop-nestings if you like..
}
}
}
// if the counter variables of the descendent-loops were set before the loop-nesting,
// the inner loops would only run once, because the counter would keep the value
// of the abortion condition after the loop is finished
回答by Femi
Do this:
做这个:
var newTimes = [];
for (var i = 0; i < times.length; i++) {
for(var x = 0; x < times[i].length; x++) {
newTimes.push(times[i][x]);
console.log(newTimes);
}
}
You are re-initializing newTimes each time through the loop.
您每次都在循环中重新初始化 newTimes。
回答by Howard
You output would be appropriate if the log statement would read
如果日志语句会读取,您的输出将是合适的
console.log(times[i][x]);
Instead you output your complete new list newTimes
which is initialized outside the inner loop and grows with each inner loop iteration.
相反,您输出完整的新列表newTimes
,该列表在内循环外初始化并随着每次内循环迭代而增长。
回答by Farshid Zaker
The problem is in the second round of the inner loop, where it pushes the second element into newTimes. Anyway I don't understand the reason of inner loop. You can write much simpler:
问题出在内部循环的第二轮中,它将第二个元素推入 newTimes。无论如何我不明白内循环的原因。你可以写得更简单:
var times = [
["04/11/10", "86kg"],
["05/12/11", "90kg"],
["06/12/11", "89kg"]
];
for (var i = 0; i < times.length; i++) {
console.log(time[i][0]);
console.log(time[i][1]);
}