Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript for loop counter coming out as string

I've simplified my program down to this, and it's still misbehaving:

var grid = [0, 1, 2, 3];

function moveUp(moveDir) {
    for (var row in grid) {
        console.log('row:');
        console.log(row + 5);
    }
}

It seems that row is a string instead of an integer, for example the output is

row:
05
row:
15
row:
25
row:
35

rather than 5, 6, 7, 8, which is what I want. Shouldn't the counter in the for loop be a string?

like image 539
stackers Avatar asked Mar 18 '14 12:03

stackers


People also ask

How do you set a counter in a for loop?

How it works. First, declare a variable counter and initialize it to 1. Second, display the value of counter in the console if counter is less than 5. Third, increase the value of counter by one in each iteration of the loop. The following example uses a for loop that has no initializer expression:

How does the JavaScript for/in statement loop through objects and arrays?

The JavaScript for/in statement loops through the properties of an Object: The JavaScript for/in statement can also loop over the properties of an Array: Do not use for in over an Array if the index order is important. The index order is implementation-dependent, and array values may not be accessed in the order you expect.

How do you loop through a string in JavaScript?

So, how exactly do you loop through a string in JavaScript? Let’s say we have a string like so: const text = new String ( "Tommy") Using the latest in ES6 syntax: [...text] .for Each (char => console.log(char)) Notice how we are spreading text into an array and then using forEach as you would for any other array type.

How to implement counters in JavaScript?

There are lots of situations where the counters can come in handy. We can implement a counter using a normal variable inside our code or using session storage. The first way of implementing counters is by using variables. For example, In the below example, we have a function named value ().


2 Answers

Quoting from MDN Docs of for..in,

for..in should not be used to iterate over an Array where index order is important. Array indexes are just enumerable properties with integer names and are otherwise identical to general Object properties. There is no guarantee that for...in will return the indexes in any particular order and it will return all enumerable properties, including those with non–integer names and those that are inherited.

Because the order of iteration is implementation dependent, iterating over an array may not visit elements in a consistent order. Therefore it is better to use a for loop with a numeric index (or Array.forEach or the non-standard for...of loop) when iterating over arrays where the order of access is important.

You are iterating an array with for..in. That is bad. When you iterate with for..in, what you get is the array indices in string format.

So on every iteration, '0' + 5 == '05', '1' + 5 == '15'... is getting printed

What you should be doing is,

for (var len = grid.length, i = 0; i < len; i += 1) {
    console.log('row:');
    console.log(grid[i] + 5);
}

For more information about why exactly array indices are returned in the iteration and other interesting stuff, please check this answer of mine

like image 139
thefourtheye Avatar answered Sep 21 '22 10:09

thefourtheye


You should use a normal for loop rather than a for...in loop for arrays.

for (var row = 0, l = grid.length; row < l; row++) {
  console.log('row:');
  console.log(5 + row);
}

I think this is what your expected output should be.

Fiddle

like image 33
Andy Avatar answered Sep 24 '22 10:09

Andy