Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Commonmark migration
Source Link

Quote from the [documentation][1]documentation:

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.

The key here holding the answer to your question is the following sentence:

Array indexes are just enumerable properties with integer names and are otherwise identical to general Object properties.

And the following sentence sums it up:

for..in should not be used to iterate over an Array where index order is important. [1]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/for...in

Quote from the [documentation][1]:

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.

The key here holding the answer to your question is the following sentence:

Array indexes are just enumerable properties with integer names and are otherwise identical to general Object properties.

And the following sentence sums it up:

for..in should not be used to iterate over an Array where index order is important. [1]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/for...in

Quote from the documentation:

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.

The key here holding the answer to your question is the following sentence:

Array indexes are just enumerable properties with integer names and are otherwise identical to general Object properties.

And the following sentence sums it up:

for..in should not be used to iterate over an Array where index order is important.

Source Link
Darin Dimitrov
  • 1.0m
  • 277
  • 3.3k
  • 3k

Quote from the [documentation][1]:

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.

The key here holding the answer to your question is the following sentence:

Array indexes are just enumerable properties with integer names and are otherwise identical to general Object properties.

And the following sentence sums it up:

for..in should not be used to iterate over an Array where index order is important. [1]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/for...in

lang-js

AltStyle によって変換されたページ (->オリジナル) /