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?
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:
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.
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.
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 ().
Quoting from MDN Docs of for..in
,
for..in
should not be used to iterate over anArray
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-standardfor...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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With