废话不多说,直接上代码:
let list = [1,2,3,4,5,6,7,8,9];
/**
* 数组的分片方法
* @param list 传入的数组
* @param size 每片的等分长度
*/
function arrSlice(list, size){
let temp = [];
list.forEach((item, index) => {
let key = Math.floor(index/size)
if(!temp[key]){
temp[key] = []
}
temp[key].push(item)
})
}
arrSlice(list, 6)