如何将数组拆分为相等的块? [英] how to split an array into equal chunks?

查看:54
本文介绍了如何将数组拆分为相等的块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于如何将数组分成多个块"这一问题,我找到了很多答案,但是我找不到最佳方式对数组进行重新分区的方法.例如,

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))

此函数为我提供了两个数组: [1,2,3,4,5,6,7,8,9,10]和[11,12,13] .但是我更希望将n用作"max"值.获得[1,2,3,4,5,6,7]和[8,9,10,11,12,13].这样,我将拥有两个相同大小的数组.如果选择的n是可能的,则它们的大小应相等,否则,两个大小几乎相同的数组.

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 to be used as a "max" to obtain [1,2,3,4,5,6,7] and [8,9,10,11,12,13]. This way I would have two arrays of the same size. If it is possible for the selected n, they should be of equal size, otherwise, two arrays with a nearly identical size.

推荐答案

我将其分解为3个步骤.

I broke it down into 3 steps.

  1. 计算 numChunks ,您需要多少块?例如.如果您有103个元素的数组且最大大小为10,那么您将需要11个块.
  2. 计算 minChunkSize ,较小块的大小.例如.在上面的示例中,前7个块将具有10个元素,而其他3个块将具有11个元素(7 10 + 3 11 = 103).
  3. 计算 numSmallChunks ,您可以拥有多少个小块.例如.在上面的示例中为3.
  1. Compute numChunks, how many chunks you need? E.g. if you have an array of 103 elements and a max size of 10, then you'll need 11 chunks.
  2. Compute minChunkSize, the size of the smaller chunks. E.g. in the above example, the first 7 chunks will have 10 elements, while the other 3 chunks will have 11 elements (710 + 311 = 103).
  3. Compute numSmallChunks, how many small chunks you can have. E.g. 3 in the above example.

然后,您只需相应地拼接 arr .

Then you just splice the arr accordingly.

let chunk = (arr, maxSize) => {
    let numChunks = parseInt((arr.length - 1) / maxSize) + 1;
    let minChunkSize = parseInt(arr.length / numChunks);
    let numSmallChunks = numChunks * (minChunkSize + 1) - arr.length;

    arr = [...arr]; // avoid muckking the input
    let arrays = [];
    for (let i = 0; i < numChunks; i++)
        if (i < numSmallChunks)
            arrays.push(arr.splice(0, minChunkSize));
        else
            arrays.push(arr.splice(0, minChunkSize + 1));

    return arrays;
};

let x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
for (let i = 1; i < x.length; i++)
  console.log(i, JSON.stringify(chunk(x, i), null, ''));

请注意,其他答案会导致不平衡;例如他们产生大小为4、4、4和&当 n 为4时为1.而我的方法会生成大小为3、3、3和&的数组.4.我想这取决于您所需要的情况,但这就是我解释问题的相等块"的方式.

Note, the other answers result in an unbalanced; e.g. they produce arrays of sizes 4, 4, 4, & 1 when n is 4. Whereas my approach produces arrays of sizes 3, 3, 3, & 4. I guess it's up to the situation which you need, but this is how I interpret the question's "equal chunks".

这篇关于如何将数组拆分为相等的块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆