将 arr1 数据结构转化为 arr2 (不足时补0,如最后一个[7 ,0])
let arr1 = [1, 2, 3, 4, 5, 6, 7];
let arr2 = [[1, 2], [3, 4], [5, 6], [7, 0]];
let arr1 = [1, 2, 3, 4, 5, 6, 7];
function calcArr(arr) {
let a = [];
let b = [];
let lastOneIndex = arr.length - 1;
arr.forEach((x, i) => {
a.push(x);
if (i % 2 !== 0 || i === lastOneIndex) {
if (i === lastOneIndex && a.length === 1) {
a.push(0)
}
b.push(a);
a = [];
}
});
return b;
}
|