Consider following code:
var arr = [111, 222, 333];
for(var i in arr) {
if(i === 1) {
// Never executed
}
}
It will fail, because typeof i === 'string'
.
Is there way around this? I could explicitly convert i
to integer, but doing so seems to defeat the purpose using simpler for in
instead of regular for
-loop.
Edit:
I am aware of using ==
in comparison, but that is not an option.
The index value represents the number of the currently executing iteration. More specifically, the body of the loop (the instructions that are executed during each loop repetition) can be said to be iterated as the loop executes.
For-loops can also be used to process strings, especially in situations where you know you will visit every character. While loops are often used with strings when you are looking for a certain character or substring in a string and do not know how many times the loop needs to run.
Yes, technically array-indexes are strings, but as Flanagan elegantly put it in his 'Definitive guide': "It is helpful to clearly distinguish an array index from an object property name. All indexes are property names, but only property names that are integers between 0 and 232-1 are indexes."
The indexing type is itself a type, so we can use unions, keyof , or other types entirely: type I1 = Person ["age" | "name"]; type I1 = string | number. type I2 = Person [keyof Person ]; type I2 = string | number | boolean.
You have got several options
Make conversion to a Number:
parseInt(i) === 1
~~i === 1
+i === 1
Don't compare a type (Use ==
instead of ===
):
i == 1 // Just don't forget to add a comment there
Change the for loop to (I would do this but it depends on what you are trying to achieve):
for (var i = 0; i < arr.length; i++) {
if (i === 1) { }
}
// or
arr.forEach(function(item, i) {
if (i === 1) { }
}
By the way you should not use for...in
to iterate through an array. See docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/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.
With ===
you compare also the type of values. Use ==
instead:
if ( i == 1 ) {}
or cast i
to integer:
if ( +i === 1 ) {}
Also you can try with 'standard' for
loop:
for (var i = 0; i < arr.length; i++) {
if (i === 1) {}
}
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