首页 > 代码库 > FCC-Chunky Monkey

FCC-Chunky Monkey

把一个数组arr按照指定的数组大小size分割成若干个数组块。

1 function chunk(arr, size) {
2   var newArr = [],newArrLittle = [];
3   for(var i=0;i<arr.length;i+=size){
4     newArrLittle = arr.slice(i,i+size);
5     newArr.push(newArrLittle);
6   }
7   return newArr;
8 }

稍微再改改,简洁一点:

1 function chunk(arr, size) {
2   var newArr = [];
3   for(var i=0;i<arr.length;i+=size){
4     newArr.push(arr.slice(i,i+size));
5   }
6   return newArr;
7 }

FCC-Chunky Monkey