Darren Smith
Darren Smith

Reputation: 23

Pop off array within Javascript for loop not getting all array items

I'm trying to figure out why the following piece of code only pops the last 3 items off the array but not the last two.

var test = ['test1','test2','test3','test4','test5'];

console.log('length - '+test.length);

for(var k = 0; k <= test.length; k++) {
    var tests = test.pop();
    console.log(tests+' - '+k);
}

The results for the above code:

length - 5
(index):30 test5 - 0
(index):30 test4 - 1
(index):30 test3 - 2

Upvotes: 2

Views: 114

Answers (5)

TbWill4321
TbWill4321

Reputation: 8666

The problem is that the length of the array is changing while the loop is executing, so the test
k <= test.length is changing with each execution.

var test = ['test1','test2','test3','test4','test5'];

console.log('length - '+test.length);

for ( var k = 0, len = test.length; k < len; k++ ) {
  var tests = test.pop();
  console.log(tests+' - '+k);
}

Or with a while loop:

var test = ['test1','test2','test3','test4','test5'];

console.log('length - '+test.length);

var k = 0;
while (test.length) {
    var tests = test.pop();
    console.log(tests+' - '+(k++));
}

Upvotes: 2

dfsq
dfsq

Reputation: 193261

Array.prototype.pop method modifies the original array, so of course for-loop will only visit half of the values until array is fully emptied.

What you can do is to use while loop instead:

var test = ['test1', 'test2', 'test3', 'test4', 'test5'];

console.log('length - ' + test.length);

while (test.length) {
  var tests = test.pop();
  console.log(tests);
}

Upvotes: 3

Maytham Fahmi
Maytham Fahmi

Reputation: 33387

If you like to keep your code style, just added following line. This is to prevent array length re-sizing in the for loop:

var arrSize = test.length;

Your final code would look like this:

<script>
    var test = ['test1', 'test2', 'test3', 'test4', 'test5'];

    var arrSize = test.length;

    console.log('length - ' + arrSize);

    for (var k = 0; k < arrSize; k++) {
        var tests = test.pop();
        console.log(tests + ' - ' + k);
    }
</script>

Upvotes: 0

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28349

Consider the logic of what you're doing... you're burning your candle at both ends (so to speak).

k = 0, length = 5 ('test1') at this point test5 is thrown away (that's what pop does, it drops the last element)

k = 1, length = 4 ('test2') (at this point test4 is thrown away)

and so on... after 3 iterations your array is 3 items.

I *think what you're trying to do you would accomplish with splice

while(myArray.length > 0){
   console.log(myArray[0])
   myArray.splice(0,1)

}

Upvotes: 1

Paul Roub
Paul Roub

Reputation: 36438

You're increasing k each time. After printing 2, k == 3 while there are still 2 elements left in the list. But since k > test.length, the loop ends.

If you really still want to keep count, increase k but don't test it:

var test = ['test1', 'test2', 'test3', 'test4', 'test5'];

console.log('length - ' + test.length);

for (var k = 0; test.length > 0; k++) {
  var tests = test.pop();
  console.log(tests + ' - ' + k);
}

Upvotes: 1

Related Questions