Why is using “for…in” for array iteration a bad idea?
The reason is that one construct: can sometimes be totally different from the other: Also consider that JavaScript libraries might do things like this, which will affect any array you create:
The reason is that one construct: can sometimes be totally different from the other: Also consider that JavaScript libraries might do things like this, which will affect any array you create:
Iterate through the entrySet() like so:
Use forEach its a built-in array function. Array.forEach():
Iterate through the entrySet() like so: Read more about Map.
Use forEach its a built-in array function. Array.forEach():
For most objects, use for .. in : With ES6, if you need both keys and values simultaneously, do To avoid logging inherited properties, check with hasOwnProperty : You don’t need to check hasOwnProperty when iterating on keys if you’re using a simple object (for example one you made yourself with {}). This MDN documentation explains more generally how to deal with objects … Read more
TL;DR Your best bets are usually a for-of loop (ES2015+ only; spec | MDN) – simple and async-friendly forEach (ES5+ only; spec | MDN) (or its relatives some and such) – not async-friendly (but see details) a simple old-fashioned for loop – async-friendly (rarely) for-in with safeguards – async-friendly Some quick “don’t”s: Don’t use for-in … Read more