相关疑难解决方法(0)

how to split an array into equal chunks?

I've found a lot of answers to the question "how to split an array in multiple chunks", but I can't find a way to best repartition the array. For example,

let x = [1,2,3,4,5,6,7,8,9,10,11,12,13];

//#Source https://www.w3resource.com/javascript-exercises/fundamental/javascript-fundamental-exercise-265.php
const chunk = (arr, size) =>
Array.from({ length: Math.ceil(arr.length / size) }, (v, i) =>
  arr.slice(i * size, i * size + size)
);

const n = 10;

console.log(chunk(x,n))
Run Code Online (Sandbox Code Playgroud)

This function gives me two arrays: [1,2,3,4,5,6,7,8,9,10] and [11,12,13]. But I would prefere n …

javascript arrays

0
推荐指数
1
解决办法
1408
查看次数

标签 统计

arrays ×1

javascript ×1