Is there an elegant, functional way to turn this array:
[ 1, 5, 9, 21 ]
into this
[ [1, 5], [5, 9], [9, 21] ]
I know I could forEach
the array and collect the values to create a new array. Is there an elegant way to do that in _.lodash
without using a forEach
?
A fast approach using
map
would be:If you're willing to use another functional library 'ramda', aperture is the function you're looking for.
Example usage taken from the ramda docs: