3 ms·
Especially when Array(N).fill(0).forEach((_, key) => ...) gives you the same exact functionality without the second eager array if you're seriously trying to av
by akiselev 7y ago
Especially when Array(N).fill(0).forEach((_, key) => ...) gives you the same exact functionality without the second eager array if you're seriously trying to avoid for loops.
- hombre_fatal 7y agoArray.from({ length: N }).forEach()
- Aeolun 7y agofor(var i = 0; i < N; i++) {}
- chii 7y agoThis is the best and cleanest way. Unless there's a reason to use Array.forEach such as automatic paralellization or some other SIMD-like optimization that can be done that cannot be done in a forloop. A lot of cargo-cult programming seem to take functional programming to mean programming using the map/reduce/foreach functions, and you end up with shitty code like [...Array(N).keys()].foreach(), just so you can somehow claim that you're doing functional programming.
- hombre_fatal 7y agoThat was already proposed upstream. The person above wanted a way to actually create an array of length N that could be used with .forEach().
- realharo 7y ago`let` instead of `var`. No need to leak the variable into the outer scope.