The Rest of the Story
This is a late entry in the answer race.
Use for..of
for ( var i of array)
automatically coerces the Array
"indexes" to numbers - so the OP's problem is fixed simply by replacing "in" with "of"
Don't use for..in
JS documentation says
Note: for...in should not be used to iterate over an Array where the index order is important.
Why? All the current answers say the problem is that the "indexes" are really strings. How is that? We all know the "for loop" uses numbers and that works.
JS Array
s are not arrays but regular JS objects
A JS Array is actually a plain old JS object in disguise - Key:Value pairs where the key (property name) is always a string. An Array
does not have real indexes! The "indexes" are just properties who's names are stringed-numbers, so "0", "1", etc. There are no numeric indexes. But you can say Array
is a special case of JS objects - it uses invalid names (starts with a digit) and JS allows us to write as if Array
actually has indexes. Just another of the many, many Javascript WTFs.