Is there an elegant, functional way to turn this array:
[ 1, 5, 9, 21 ]
into this
[ [1, 5], [5, 9], [9, 21] ]
I kn
You may do as follows with just a sinle liner of .reduce() with no initial;
.reduce()
var arr = [ 1, 5, 9, 21 ], pairs = arr.reduce((p,c,i) => i == 1 ? [[p,c]] : p.concat([[p[p.length-1][1],c]])); console.log(pairs);