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

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

 

posted @ 2019-04-23 23:24  Haoyin-杰克  阅读(730)  评论(0编辑  收藏  举报