[removed] Make an array of value pairs form an array of values

前端 未结 8 1745
灰色年华
灰色年华 2021-01-06 08:40

Is there an elegant, functional way to turn this array:

[ 1, 5, 9, 21 ]

into this

[ [1, 5], [5, 9], [9, 21] ]

I kn

8条回答
  •  -上瘾入骨i
    2021-01-06 09:02

    A fast approach using map would be:

    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; }

提交回复
热议问题