简体   繁体   English

这个 for 循环数组是如何工作的?

[英]How does this for loop array works?

I just learn about array today but I don't actually understand this code I'm learning.我今天刚学习数组,但我实际上并不理解我正在学习的这段代码。

let units = [1, 10, 100, 1000, 20];

for(let i = 0; i < units.length; i += 1){
    console.log(units[i]);
}

How would console.log(units[i]);怎么会console.log(units[i]); return 1 , 10 , 100 , 1000 , 20 and not 5 ?返回110100100020而不是5

Could you guys explain to me the way I will actually understand this?你们能向我解释一下我真正理解这一点的方式吗?

units is an array. units是一个数组。

units[0] gets you the first item in the array (1). units[0]为您提供数组 (1) 中的第一项。 units[1] gets you the next item in the array (10). units[1]为您获取数组 (10) 中的下一项。 and so on.等等。

So as i is incremented with each loop iteration, you get the corresponding item from the list.因此,当i随着每次循环迭代而递增时,您会从列表中获得相应的项目。

Just for your information.仅供参考。

This below is the initialization of list of numbers in a variable units下面是变量units中数字列表的初始化

let units = [1, 10, 100, 1000, 20];

Now the loop begin.现在循环开始。

for() is the loop. for()是循环。

Where 1st params let i=0 tell the starting point of the array.其中第一个参数let i=0告诉数组的起点。

2nd params i<units.length defined the length of array where the loop needs to be end第二个参数i<units.length定义了循环需要结束的数组的长度

3rs params tell the index need to be increment each time the loop executed. 3rs 参数告诉索引需要在每次循环执行时递增。 For eg: when index i is 0, then value units[i] = 1 .例如:当索引i为 0 时,则值units[i] = 1 After its execution the value of index i is incremented by 1 due to i++ as third param of for() loop.执行后,由于i++作为for()循环的第三个参数,索引i的值增加 1。

The loop will continue until it meets the value of last index defined in second params that is i<units.length循环将继续,直到它满足第二个参数中定义的最后一个索引的值,即i<units.length

for(let i = 0; i < units.length; i += 1){
    console.log(units[i]);
}

I hope it made you clear.我希望它让你清楚。

You are both declaring that a variable units exists and you are putting elements in the array with your statement你们都声明了一个变量units存在并且你正在用你的语句将元素放入数组中

let units = [1, 10, 100, 1000, 20];

This has created an array with 5 elements;这创建了一个包含 5 个元素的数组; array elements are numbered (indexed) starting at zero :数组元素从零开始编号(索引):

| 1 | 10 | 100 | 1000 | 20 |     // array with elements in it
  0   1    2     3      4        // positions of elements

— the first element in the array is at position 0 — 数组中的第一个元素位于 position 0
that is, the item at units[0] is 1 , the item at units[1] is 10 , etc.也就是说,位于units[0]的项目是1 ,位于units[1]的项目是10 ,等等。

Now you start your loop, using the variable i as a number to use as an index into the array.现在开始循环,使用变量i作为数字用作数组的索引

When i === 0 then units[i] is asking for the element at position units[0] , which is 1,i === 0时, units[i]要求位于 position units[0]的元素,即 1,
when i === 1 then units[i] is asking for the element at position units[1] , which is 10,i === 1时, units[i]要求位于 position units[1]的元素,即 10,
and so on.等等。

So when you say console.log(units[i]);所以当你说console.log(units[i]); you are not printing the value of i ;没有打印i的值; you are getting the element in the array units that is at position i in the array and printing that .您正在获取数组中位于position i的数组units中的元素并打印
console.log(units[i]) when i === 3 gets the item in the array at position 3, which is 1000 , so it prints 1000. console.log(units[i])i === 3获取数组中位于 position 3 的项目时,它是1000 ,因此它打印 1000。

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

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