js小方法积累,将一个数组按照n个一份,分成若干数组

时间:2024-01-09 21:13:44
 // 把一个数组按照一定长度分割成若干数组
function group(array, subGroupLength) {
let index = 0;
let newArray = [];
while(index < array.length) {
newArray.push(array.slice(index, index += subGroupLength));
}
return newArray;
} let numberArray = [1,2,3,4,5,6,7,8,9,10,11,12];;
let groupedArray = group(numberArray, 5);
console.log(groupedArray);

打印结果为:

js小方法积累,将一个数组按照n个一份,分成若干数组