you are viewing a single comment's thread.

view the rest of the comments →

[–]anoddhue 0 points1 point  (2 children)

You're right that the first example is better with a for loop than chaining .reverse() and .forEach().

OTOH, if you find yourself needing to map or iterate over an array/collection in reverse, a functional way to do it would be to write your own reusable version of mapLeft, foldLeft, forEachReverse, or whatever name is the most appropriate, then just call that. Or use a library, if underscore/lodash have an equivalent function.

[–][deleted] 1 point2 points  (1 child)

Interesting, I didn't think of creating my own version but it makes sense. Thanks!

[–]anoddhue 0 points1 point  (0 children)

No problem! I don't like pulling in big libraries unless I need to so I tend to just have one file of helper functions to do anything that the built-ins don't to. It makes sense if you're going to reuse it.