My favorite has become Array.prototype.reduce. I haven't written JS seriously in over two years, though. Reduce is so general purpose that it can be used to mimic all of the above. I resort to this often if I need a new data type, like performing some operation on the elements in the array and dumping them into a map or object or simply to avoid mutating the original input array
One thing that may also be worth noting is that all of the above should only be used if the intention is to enumerate the entire array with no early termination conditions. In those scenarios, one must use the standard for-loop, while loop, or reduce (skipping past values on conditions).
My favorite has become
Array.prototype.reduce
. I haven't written JS seriously in over two years, though. Reduce is so general purpose that it can be used to mimic all of the above. I resort to this often if I need a new data type, like performing some operation on the elements in the array and dumping them into a map or object or simply to avoid mutating the original input arrayOne thing that may also be worth noting is that all of the above should only be used if the intention is to enumerate the entire array with no early termination conditions. In those scenarios, one must use the standard for-loop, while loop, or reduce (skipping past values on conditions).
Nice write-up, though!
You're absolutely right. Array.prototype.reduce has been a major helper in dire situations.