简体   繁体   English

使用 for 循环对数组进行迭代真的有效吗?

[英]How does iterating over an array using a for loop REALLY work?

I am a javascript beginner and I understand how to iterate over an array and how to use it, but I am trying really, really hard to understand WHY it works.我是 javascript 初学者,我了解如何迭代数组以及如何使用它,但我真的非常非常难以理解它为什么起作用。

For example:例如:

let myArray = ["one", "two", "three", "four"];

for(let i = 0; i < myArray.length; i++){ 

console.log(myArray[i]);
}

I understand what is going on in each of the 3 parts within the for loop, but I don't really understand how the i is accessing/communicating with/connected to/exchanging data with the array myArray.我了解 for 循环中的 3 个部分中的每一部分发生了什么,但我并不真正了解i如何访问/通信/连接到/交换数组myArray. At which point in this code are we telling javascript that "i" is somehow connected to "myArray"?在这段代码中,我们告诉 javascript “i”以某种方式连接到“myArray”?

At first I thought something was implied or implicit in the for loop itself, ie, that when we write i < myArray.length it is somehow implying that i = myArray (that "i" is assigned to the value of whatever is in myArray ).起初,我认为 for 循环本身隐含或隐含了某些内容,即,当我们编写i < myArray.length时,它以某种方式暗示i = myArray (“i”被分配给myArray中的任何值) . But upon further thought, i < myArray.length is simply the length of the array (in this case, 4), and doesn't really connect the two.但经过进一步思考, i < myArray.length只是数组的长度(在本例中为 4),并没有真正将两者联系起来。

So this has opened up a whole conceptual can of worms for me about what the "i" really is here besides just a variable in a for loop.因此,除了 for 循环中的变量之外,这为我打开了一整套关于“i”真正在这里的概念。 I have been thinking about the "i" as a sort of ghost/temporary variable that we create that will do the looping for us and then disappear once it is done (I am not even sure if that is the correct metaphor here).我一直在考虑将“i”视为我们创建的一种幽灵/临时变量,它将为我们执行循环,然后在完成后消失(我什至不确定这是否是正确的比喻)。

I apologize in advance if I am not articulating this clearly, as I am just a beginner.如果我没有清楚地表达这一点,我提前道歉,因为我只是一个初学者。

Thanks in advance.提前致谢。

The variable i is simply just a variable.变量i只是一个变量。 It is a number that is incremented with each iteration, and stops incrementing when it increments through all the indexes in the array.它是一个随着每次迭代递增的数字,当它递增到数组中的所有索引时停止递增。 The variable i is not in connected with myArray except that it loops through all the indexes of myArray (0, 1, 2, 3).变量 i 与 myArray 无关,只是它循环遍历 myArray 的所有索引 (0, 1, 2, 3)。 It starts with 0, and increments itself while doing the commands inside the for loop (getting a certain element in myArray using bracket notation).它从 0 开始,并在执行 for 循环内的命令时自增(使用括号表示法获取 myArray 中的某个元素)。

Eg, the variable i starts at a value of zero.例如,变量i从零值开始。 While being 0, myArray is called in the for loop, but specifically for the 0th index, which is "one".当为 0 时,在 for 循环中调用myArray ,但专门针对第 0 个索引,即“一”。 Then the variable i increments, and the code in the for loop calls the 1st index of the array, and so on until the conditional i < myArray.length is met, which marks the upper boundary of the indexes of the array.然后变量i递增,for 循环中的代码调用数组的第一个索引,以此类推,直到满足条件 i < myArray.length,这标志着数组索引的上边界。

I hope this helps!我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM