While I generally agree with the message here (using Object.keys(), Object.values() and Object.entries()), I wouldn't generally recommend using for of loop (or any for loop for that matter). It doesn't work well with immutable data. Using pure functions like Array.map(), Array.forEach() or Array.filter() is generally better idea - you don't have to deal with side effects and it's generally easier to read.
I totally agree with you regarding for loop and how .map, .reduce, .filter are a heavenly match for functional programming. But there are cases where I feel for of loop is a natural fit:
Iterator: Javascript now has native support for iterators (I talk about them in my article), so dealing with Map, Set, Array etc, the for of loop becomes a natural choice over converting these iterators to arrays and then applying the functional methods.
Async: Another area I find for of loop a rather easier read is when I have to iterate through an async collection. I talk about some examples of async for loop here.
I'd say this has more to do with your style, while I prefer these methods also, I've seen bunch of colleagues not understanding how these methods flow
it feels magical to them, they rather have a for-of (or a good old for) perhaps because my colleagues do tend to do more procedural stuff but y' know what works best for you is the way to go most of the time
but to reasure, I do prefer to use map, forEach, filter, and reduce when possible
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
While I generally agree with the message here (using
Object.keys()
,Object.values()
andObject.entries()
), I wouldn't generally recommend usingfor of
loop (or anyfor
loop for that matter). It doesn't work well with immutable data. Using pure functions likeArray.map()
,Array.forEach()
orArray.filter()
is generally better idea - you don't have to deal with side effects and it's generally easier to read.I totally agree with you regarding
for loop
and how.map
,.reduce
,.filter
are a heavenly match for functional programming. But there are cases where I feelfor of
loop is a natural fit:Iterator: Javascript now has native support for iterators (I talk about them in my article), so dealing with
Map
,Set
,Array
etc, thefor of
loop becomes a natural choice over converting these iterators to arrays and then applying the functional methods.Async: Another area I find
for of
loop a rather easier read is when I have to iterate through an async collection. I talk about some examples of async for loop here.I'd say this has more to do with your style, while I prefer these methods also, I've seen bunch of colleagues not understanding how these methods flow
it feels magical to them, they rather have a for-of (or a good old for) perhaps because my colleagues do tend to do more procedural stuff but y' know what works best for you is the way to go most of the time
but to reasure, I do prefer to use map, forEach, filter, and reduce when possible