You’re reading an excerpt of my upcoming book on clean code, “Washing your code: write once, read seven times.” Preorder it on Leanpub or read a ...
For further actions, you may consider blocking this person and/or reporting abuse
while I don't find
for ofloops less readable than any functional array transformation.I use functional methods heavily for array transformations or when I use React.
Other than that I find loops more readable when logic is longer than 3, 4 line or contains many if/else.
In async stuff loops are much better because in map/reduce you need to await the map/reduce and make the inner function async and also await inside it.
for ex:
The reduce in your example is bad because it is not a reduce, its should be a map, reduce is for reducing the entirety to one value normally to sum or average or something.
Also using the
function(){}syntax is still useful if you give the function a name, helps give it some extra semantics over the use of arrow functions.is missing the initial value for
reduce, such as a[].And like @samuraiseoul mentioned, this is a case for a flatMap.
If you don't have your own
flatMapyet, store bought is fine:JK, don't use this one, it's probably not very good.
Thanks for pointing the missing initializer, fixing!
Object#fromEntries()to the rescue: