将一个数组分割成多个

function sliceArray(array, size) {
  var result = [];
  for (var x = 0; x < Math.ceil(array.length / size); x++) {
    var start = x * size;
    var end = start + size;
    result.push(array.slice(start, end));
  }
  return result;
}
let list = [1,2,3,4,5,6,7,8]
console.log(sliceArray(list,3)); //[[1,2,3],[4,5,6],[7,8]]
 

 

posted @ 2022-07-12 10:49  前进中的蜗牛  阅读(220)  评论(0编辑  收藏  举报