了解JavaScript中的嵌套循环 [英] Understanding nested for loops in javascript
问题描述
此刻我正在freecodecamp上学习JavaScript,他们在其中的一个练习中有一个嵌套循环的示例:
I'm learning JavaScript at the moment on freecodecamp and they have an example for nested for loops in one of their excercises:
var arr = [[1,2], [3,4], [5,6]];
for (var i=0; i < arr.length; i++) {
for (var j=0; j < arr[i].length; j++) {
console.log(arr[i][j]);
}
}
使用console.log = 1 2 3 4 5 6未定义.
With console.log = 1 2 3 4 5 6 undefined.
我或多或少了解for循环,并且我了解[i]和[j]用于访问数组(我认为吗?).我只是不明白为什么最后只打印出这些数字?我发现这个问题是在几年前问的,但这只是解释了如何编写它们,而不是它们是如何工作的:
I understand for loops more or less, and I understand that [i] and [j] are used to access the array (I think?). I just don't understand why at the end it just prints out those numbers? I found this question asked a few years back but it just explains how to write them, not how they work:
我将其分解为:
var arr = [ [1,2], [3,4], [5,6]];for (var i=0; i < arr.length; i++) {
console.log(arr[i]);}
哪个打印出
[ 1, 2 ]
[ 3, 4 ]
[ 5, 6 ]
undefined
和
var arr = [ [1,2], [3,4], [5,6]];
for (var i=0; i < arr.length; i++) {
for (var j=0; j < arr[i].length; j++) {
console.log(arr[i]); }}
打印出的内容:
[ 1, 2 ]
[ 1, 2 ]
[ 3, 4 ]
[ 3, 4 ]
[ 5, 6 ]
[ 5, 6 ]
undefined
和
var arr = [ [1,2], [3,4], [5,6]];
for (var i=0; i < arr.length; i++) {
for (var j=0; j < arr[i].length; j++) {
console.log(arr[j]); }}
打印出来的
[ 1, 2 ]
[ 3, 4 ]
[ 1, 2 ]
[ 3, 4 ]
[ 1, 2 ]
[ 3, 4 ]
undefined
我了解前两个arr [i].循环遍历数组,并打印出各个元素(在本例中为数组),在第二个中,我猜它只做了两次,因为有两个循环.我不明白的是:
I understand the first two arr[i]. The loop iterates through the array and prints out the individual elements (in this case an array) and in the second one I guess it just does it twice because there are two loops. What I don't understand is:
- 为什么没有打印出arr [j]中的最后一个数组( [5,6]去吗?)
- 为什么arr [i] [j]突然消除了数组而只是 打印出数字
- 未定义"来自哪里
- why the last array in arr[j] is not printed out (where did the [5, 6] go?)
- why arr[i][j] suddenly eliminates the arrays and just prints out the numbers
- where the 'undefined' comes from
有人可以帮助我解决这个问题,并解释在控制台中将其打印出来之前代码所采取的步骤吗?我真的很想了解它,但是甚至都不知道如何以正确的方式搜索这个问题.
Could anyone help me out with this and explain the steps the code takes before printing it out in the console? I would really like to understand it but don't even know how to search for this question the right way.
推荐答案
var arr = [[1,2], [3,4], [5,6]];
这是一个数组数组.像这样阅读起来会更容易一些:
This is an array of arrays. It is a little bit easier to read like this:
var arr = [
[1,2],
[3,4],
[5,6]
];
这样一来,您可以轻松地看到一个由3个数组组成的数组.外部的"for"将循环遍历每个第一级数组.因此,当i = 0时,第一个外部for循环将获取第一个内部数组[1,2]:
That makes it a little bit easier to see that you have an array of 3 arrays. The outer 'for' will loop through each of 1st level arrays. So the very first outer for loop when i=0 you are going to grab the first inner array [1,2]:
for (var i=0; i < arr.length; i++) {
//First time through i=0 so arr[i]=[1,2];
}
在内部循环中,您将一次循环遍历3个内部阵列中的每一个.
In the inner loop you are going to loop through each of the 3 inner arrays one at a time.
for (var j=0; j < arr[i].length; j++) {
//Handle inner array.
}
此参数获取内部数组的长度:
This argument grabs the length of the inner array:
arr[i].length
因此,第一次通过外循环i = 0时,arr [i]等于[1,2],因为您要获取第0个元素.请记住,数组元素总是从0开始计数,而不是从1开始.
So on your first time through the outer loop i=0 and arr[i] is going to equal [1,2] because you are grabbing the 0th element. Remember, arrays elements are always counted starting at 0, not 1.
最后,您将使用以下命令打印结果:
Finally you are printing out the results with:
console.log(arr[i][j]);
第一次,您可以将其分解. i = 0和j = 0. arr [0] [0]转换为从外部数组中获取第一个元素,然后从第一内部数组中获取第一个元素.在这种情况下,它是"1":
The first time through you can break it down a little. i=0 and j=0. arr[0][0] which translates as grab the first element from the outer array and then the first element from the first inner array. In this case it is '1':
[
[1,2], <-- 0
[3,4], <-- 1
[5,6] <-- 2
];
该代码将循环遍历第一个第一组[1,2],然后遍历第二个[3,4],依此类推.
The code will loop through the first first set [1,2], then the second [3,4], and so on.
这篇关于了解JavaScript中的嵌套循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!