简体   繁体   English

Array.length在for循环中未产生预期结果

[英]Array.length not producing expected result in for loop

So I was doing a question which involves reversing an array. 所以我在做一个涉及反转数组的问题。 It takes an array as an argument and produces a new array that has the same elements in the inverse order. 它以数组作为参数,并产生一个新数组,该数组具有相反的顺序相同的元素。 This is what I came up with at first. 这是我起初想到的。

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var array = [];

for(var i = 0; i < fruits.length; i++){
 array.push(fruits.pop(i))
}
console.log(array);

The result i ended up getting was ["Mango", "Apple"] which is not what i was hoping for. 我最终得到的结果是["Mango", "Apple"] ,这不是我想要的。 So i tried this instead: 所以我尝试了这个:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var array = [];
var count = fruits.length;

for(var i = 0; i < count; i++){
 array.push(fruits.pop(i))
}
console.log(array);

This version gave me what I wanted ["Mango", "Apple", "Orange", "Banana"] . 这个版本给了我想要的东西["Mango", "Apple", "Orange", "Banana"] So the only difference between them is that in the first one I use fruits.length directly in the for loop and I assign the value of fruits.length to count in the second one which I then use in the for loop. 所以他们之间的唯一区别是,在第一个我用fruits.length在直接循环,我给你的价值fruits.lengthcount的第二个,然后我在使用循环。 I don't understand why they produce two different results. 我不明白为什么他们会产生两种不同的结果。 Aren't they the same thing? 他们不是同一回事吗? Can someone please help understand what's going on here? 有人可以帮忙了解一下发生了什么吗?

You are removing a fruit from your array ie fruits.pop(i) , which will decrement the fruits.length . 您正在从数组中删除一个水果,例如fruits.pop(i) ,这将减少fruits.length Which is then evaluated every time in the loop. 然后在循环中每次对其进行评估。

So it makes perfectly sense that your second example works correctly. 因此,您的第二个示例可以正常工作完全有道理。 Here you set the length once and once only. 在这里,您一次只能设置一次长度。

Using fruits.pop() removes the last element from the array, so on the first pass through, i = 0 and fruits.length = 4, next pass i = 1 and fruits.length = 3, next pass i = 2 and fruits.length also = 2 so your i < fruits.length check is no longer passing and the loop ends. 使用fruits.pop()会从数组中删除最后一个元素,因此在第一次通过时, i = 0和fruits.length = 4,下一次传递i = 1和fruits.length = 3,接下来的传递i = 2和fruits.length也= 2,因此您的i < fruits.length检查不再通过,循环结束。

In your second example, you are pre-defining the length to equal 4 and that doesn't change as you are removing elements from the array, so the loop continues while i < 4. 在第二个示例中,您将长度预定义为等于4,并且在从数组中删除元素时该长度不变,因此在i <4时循环继续。

Basically, array.length will be evaluated in every loop, and you're changing the array size every time by -1. 基本上, array.length将在每个循环中求值,并且每次将数组大小更改-1。 Since you're assigning the count to a variable before entering the loop, the variable won't change. 由于您是在进入循环之前将计数分配给变量,因此该变量不会更改。

Let's break it down: 让我们分解一下:

Your array has 4 items, your counter is 0. You iterate once, remove one item from the array and the counter goes up by 1. Now array.length is 3. 您的数组有4个项目,您的计数器为0。您迭代一次,从数组中删除一个项目,然后计数器增加1。现在array.length为3。

Same thing happens, now counter is 2 and array.length is 2. 发生同样的事情,现在计数器是2, array.length是2。

When trying to run for the third time, the conditional will not match and the code won't run. 尝试第三次运行时,条件将不匹配,并且代码将无法运行。 That's why the array is having two elements only. 这就是数组仅包含两个元素的原因。

Hm, You are using pop() method which removes an accessed element from an array after it has been read. 嗯,您正在使用pop()方法,该方法会在读取数组后从数组中删除该元素。

In the beginning, you have 4 elements. 首先,您有4个元素。

So loop starts to access the last element and removes it from fruits and then add to a new array. 因此,循环开始访问最后一个元素,并将其从fruits移除,然后添加到新数组中。
At next loop length is 3 instead 4. 在下一个循环长度为3而不是4。
then length is 2 and this is a place where the loop has not meet the condition for continue. 然后length为2,这是循环未满足继续条件的地方。

If you store count number in a variable it will not be affected in latter array count. 如果将计数编号存储在变量中,则以后的数组计数将不受影响。

Firstly, Array.pop does not acknowledges any parameter. 首先, Array.pop不确认任何参数。 Hence, fruits.pop(i) & fruits.pop() are one and the same thing. 因此, fruits.pop(i)fruits.pop()是一回事。

Understanding 1st Loop 了解第一环

for(var i = 0; i < fruits.length; i++){
 array.push(fruits.pop(i))
}

For i = 0 , fruits = ["Banana", "Orange", "Apple", "Mango"] , fruits.length = 4 -> i < fruits.length is TRUE , enters loop and pops out 1 value 对于i = 0fruits = ["Banana", "Orange", "Apple", "Mango"]fruits.length = 4 > i <fruits.lengthTRUE ,进入循环并弹出1个值

For i = 1 , fruits = ["Banana", "Orange", "Apple"] , fruits.length = 3 -> i < fruits.length is TRUE , enters loop and pops out 1 value 对于i = 1fruits = ["Banana", "Orange", "Apple"]fruits.length = 3 > i <fruits.lengthTRUE ,进入循环并弹出1个值

For i = 2 , fruits = ["Banana", "Orange"] , fruits.length = 2 -> i < fruits.length is FALSE , breaks the loop 对于i = 2fruits = ["Banana", "Orange"]fruits.length = 2 > i <fruits.lengthFALSE ,中断了循环


Understanding 2nd Loop 了解第二循环

for(var i = 0; i < count; i++){
 array.push(fruits.pop(i))
}

For i = 0 , fruits = ["Banana", "Orange", "Apple", "Mango"] , count = 4 -> i < count is TRUE , enters loop and pops out 1 value 对于i = 0fruits = ["Banana", "Orange", "Apple", "Mango"]count = 4 > i <countTRUE ,进入循环并弹出1个值

For i = 1 , fruits = ["Banana", "Orange", "Apple"] , count = 4 -> i < count is TRUE , enters loop and pops out 1 value 对于i = 1fruits = ["Banana", "Orange", "Apple"]count = 4 > i <countTRUE ,进入循环并弹出1个值

For i = 2 , fruits = ["Banana", "Orange"] , count = 4 -> i < count is TRUE , enters loop and pops out 1 value 对于i = 2fruits = ["Banana", "Orange"]count = 4 > i <计数TRUE ,进入循环并弹出1个值

For i = 3 , fruits = ["Banana"] , count = 4 -> i < count is TRUE , enters loop and pops out 1 value 对于i = 3fruits = ["Banana"]count = 4 > i <计数TRUE ,进入循环并弹出1个值

For i = 4 , fruits = [] , count = 4 -> i < count is FALSE , breaks the loop 对于i = 4fruits = []count = 4 > i <countFALSE ,中断循环

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

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