Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for( in ) loop index is string instead of integer

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.

like image 351
user694733 Avatar asked Jun 06 '14 08:06

user694733


People also ask

What is the index in a for loop?

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.

Can a for loop be used for strings?

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.

Are typescript arrays indexed with integers or strings?

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."

What is type of index in JavaScript?

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.


2 Answers

You have got several options

  1. Make conversion to a Number:

    parseInt(i) === 1
    ~~i === 1
    +i === 1
    
  2. Don't compare a type (Use == instead of ===):

    i == 1 // Just don't forget to add a comment there
    
  3. 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.

like image 182
0101 Avatar answered Sep 18 '22 04:09

0101


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) {}
}
like image 40
hsz Avatar answered Sep 21 '22 04:09

hsz