将JS数组拆分为N个数组

想象一下,我有一个像这样的JS数组:


var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];

我想要的是将该数组拆分为N个较小的数组。例如:


split_list_in_n(a, 2)

[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11]]


For N = 3:

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11]]


For N = 4:

[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11]]


For N = 5:

[[1, 2, 3], [4, 5], [6, 7], [8, 9], [10, 11]]

对于Python,我有这个:


def split_list_in_n(l, cols):

    """ Split up a list in n lists evenly size chuncks """

    start = 0

    for i in xrange(cols):

        stop = start + len(l[i::cols])

        yield l[start:stop]

        start = stop

对于JS,我可以提出的最佳解决方案是递归函数,但我不喜欢它,因为它既复杂又丑陋。这个内部函数返回一个像这样的数组[1,2,3,null,4,5,6,null,7,8],然后我必须再次循环并手动拆分它。(我的第一次尝试是返回此:[1、2、3,[4、5、6,[7、8、9]]],然后我决定使用null分隔符进行操作。)


function split(array, cols) {

    if (cols==1) return array;

    var size = Math.ceil(array.length / cols);

    return array.slice(0, size).concat([null]).concat(split(array.slice(size), cols-1));

}

这是一个jsfiddle:http : //jsfiddle.net/uduhH/


你会怎么做?谢谢!


LEATH
浏览 1787回答 3
3回答

智慧大石

您可以将切片设为“平衡”(子数组的长度差异尽可能小)或“偶数”(除了最后一个的所有子数组都具有相同的长度):function chunkify(a, n, balanced) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if (n < 2)&nbsp; &nbsp; &nbsp; &nbsp; return [a];&nbsp; &nbsp; var len = a.length,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out = [],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i = 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size;&nbsp; &nbsp; if (len % n === 0) {&nbsp; &nbsp; &nbsp; &nbsp; size = Math.floor(len / n);&nbsp; &nbsp; &nbsp; &nbsp; while (i < len) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.push(a.slice(i, i += size));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; else if (balanced) {&nbsp; &nbsp; &nbsp; &nbsp; while (i < len) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size = Math.ceil((len - i) / n--);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.push(a.slice(i, i += size));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; n--;&nbsp; &nbsp; &nbsp; &nbsp; size = Math.floor(len / n);&nbsp; &nbsp; &nbsp; &nbsp; if (len % size === 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size--;&nbsp; &nbsp; &nbsp; &nbsp; while (i < size * n) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.push(a.slice(i, i += size));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; out.push(a.slice(size * n));&nbsp; &nbsp; }&nbsp; &nbsp; return out;}///////////////////////onload = function () {&nbsp; &nbsp; function $(x) {&nbsp; &nbsp; &nbsp; &nbsp; return document.getElementById(x);&nbsp; &nbsp; }&nbsp; &nbsp; function calc() {&nbsp; &nbsp; &nbsp; &nbsp; var s = +$('s').value, a = [];&nbsp; &nbsp; &nbsp; &nbsp; while (s--)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.unshift(s);&nbsp; &nbsp; &nbsp; &nbsp; var n = +$('n').value;&nbsp; &nbsp; &nbsp; &nbsp; $('b').textContent = JSON.stringify(chunkify(a, n, true))&nbsp; &nbsp; &nbsp; &nbsp; $('e').textContent = JSON.stringify(chunkify(a, n, false))&nbsp; &nbsp; }&nbsp; &nbsp; $('s').addEventListener('input', calc);&nbsp; &nbsp; $('n').addEventListener('input', calc);&nbsp; &nbsp; calc();}<p>slice <input type="number" value="20" id="s"> items into<input type="number" value="6" id="n"> chunks:</p><pre id="b"></pre><pre id="e"></pre>

慕尼黑5688855

我认为使用拼接的这种方式最干净:splitToChunks(array, parts) {&nbsp; &nbsp; let result = [];&nbsp; &nbsp; for (let i = parts; i > 0; i--) {&nbsp; &nbsp; &nbsp; &nbsp; result.push(array.splice(0, Math.ceil(array.length / i)));&nbsp; &nbsp; }&nbsp; &nbsp; return result;}例如,对于parts = 3,您将占剩余部分的1/3,然后是1/2,然后是数组的其余部分。Math.ceil确保在元素数量不均匀的情况下,它们将排到最早的块。(注意:这会破坏初始数组。)
打开App,查看更多内容
随时随地看视频慕课网APP