目标数组 [1, 2, 3, 4, 5, 6, 7]
目标结果 [[1, 2], [3, 4], [5, 6], [7]]
想到了一种最简单,但好像有点麻烦,有没有更好的方法
const arr = [1, 2, 3, 4, 5, 6, 7];
arr
.map((item, index) => {
if (index % 2 === 0) {
return [item, arr[index + 1]].filter(item=>item !== undefined);
} else {
return null;
}
})
.filter((item) => item !== null);