分割数组
/**
*根据指定的分割数,将数组(字符串)分割成块,剩余数量不足以达到分割数的亦形成一个数组
*
* @param {Array} array 获取的数组
* @param {number} [size=1] 每一块的数量
* @example
*
* _.chunk([1,2,3,4,5],2);
* //=>[[1,2],[3,4],[5]]
*/
function chunk(array,size) {
let length = array == null ? 0 : array.length;
if (!length || size < 1){
return []
}
let index = 0,
resIndex = 0,
result = Array(Math.ceil(length / size));
while(index < length){
result[resIndex++] = array.slice(index, (index += size));
}
return result
}