Is there an elegant, functional way to turn this array:
[ 1, 5, 9, 21 ]
into this
[ [1, 5], [5, 9], [9, 21] ]
I kn
A fast approach using map would be:
map
const arr = [ 1, 5, 9, 21 ]; const grouped = arr.map((el, i) => [el, arr[i+1]]).slice(0, -1); console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }