Chunky Monkey
猴子吃香蕉可是掰成好几段来吃哦!
把一个数组arr
按照指定的数组大小size
分割成若干个数组块。
例如:chunk([1,2,3,4],2)=[[1,2],[3,4]];
chunk([1,2,3,4,5],2)=[[1,2],[3,4],[5]];
function chunk(arr, size) {// Break it up.
var arr1=[];
var i=0;
while(i<arr.length){
arr1.push(arr.slice(i,i+size));
i+=size;
}
return arr1;
}
用到的知识:array.push();//关于push的详解:http://blog.csdn.net/golden_wheat/article/details/53231810
array.slice();//关于slice的详解:http://blog.csdn.net/golden_wheat/article/details/53178088