const bigArr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
const arr = [[],[],[],[],[],[]]; // 定义接收数组
const counts = bigArr.length % arr.length;
let remains = 0;
if(counts === 0){
remains = bigArr.length / arr.length;
}else{
remains = parseInt(bigArr.length / (arr.length));
}
let i = 1;
let j = 1;
bigArr.map((t,index)=>{
if(index < remains * i){ // 均分逻辑
arr[i-1].push(t);
}else if(index === (remains*i)){
if(i<arr.length)
{
arr[i].push(t);
i++;
}else if(i === arr.length){
arr[0].push(t);// 超出第一条加在第一个数组
}
}else if(counts !== 0){ // 存在余数逻辑
arr[j].push(t);
j++;
}
});
console.log('输出结果是--->',arr)
JS将大数组均分给每个小数组的算法
于 2022-03-07 14:10:00 首次发布