拆分JS数组为N阵列 [英] Splitting a JS array into N arrays
问题描述
想象一下,我有一个JS数组是这样的:
变种一个= [1,2,3,4,5,6,7,8,9,10,11];
我要的就是这个数组分成N个较小的阵列。例如:
split_list_in_n(一,二)
[[1,2,3,4,5,6],[7,8,9,10,11]对于N = 3:
[[1,2,3,4],[5,6,7,8],[9,10,11]对于N = 4:
[[1,2,3],[4,5,6],[7,8,9],[10,11]]对于N = 5:
[[1,2,3],[4,5],[6,7],[8,9],[10,11]]
对于Python,我有这样的:
高清split_list_in_n(L,COLS):
分手了名单的N名单均匀大小chuncks
启动= 0
在的xrange(COLS)I:
停止=启动+ LEN(L [我:: COLS])
产量L [启动:停止]
启动=停止
有关JS,我能拿出最好的正确的解决方案是一个递归函数,但由于它的复杂和丑陋,我不喜欢它。此内部函数返回一个这样的数组[1,2,3,空,4,5,6,空,7,8],然后我不得不再次循环和手动拆分它。 (我的第一次尝试返回此:[1,2,3,[4,5,6,[7,8,9]]],我决定与空隔板这样做)。
函数split(数组,COLS){
如果(COLS == 1)返回的数组;
VAR大小= Math.ceil(array.length / COLS);
返回array.slice(0,大小).concat([空])CONCAT(分(array.slice(大小),COLS-1))。
}
下面是一个的jsfiddle: http://jsfiddle.net/uduhH/
你会怎么做呢?谢谢!
您可以切片平衡(子阵'的长度不同,尽可能少)或偶(所有子阵,但最后有相同的长度)
函数chunkify(A,N,平衡){\r
\r
如果(正2)\r
返回[一个];\r
\r
VAR LEN =则为a.length,\r
OUT = [],\r
I = 0,\r
尺寸;\r
\r
如果(LEN%N === 0){\r
大小= Math.floor(LEN / N);\r
而(I< LEN){\r
out.push(a.slice(I,I + =大小));\r
}\r
}\r
\r
否则,如果(平衡){\r
而(I< LEN){\r
大小= Math.ceil((LEN - I)/ N--);\r
out.push(a.slice(I,I + =大小));\r
}\r
}\r
\r
其他{\r
\r
N--;\r
大小= Math.floor(LEN / N);\r
如果(LEN%大小=== 0)\r
尺寸 - ;\r
而(I<大小* N){\r
out.push(a.slice(I,I + =大小));\r
}\r
out.push(a.slice(大小* N));\r
\r
}\r
\r
返回的;\r
}\r
\r
\r
///////////////////////\r
\r
的onload =函数(){\r
函数$(X){\r
返回的document.getElementById(X);\r
}\r
\r
函数计算(){\r
。变种S = + $('S')值,A = [];\r
而(S--)\r
a.unshift(多个);\r
变种N = + $('N')值。\r
$('B')。=的textContent JSON.stringify(chunkify(A,N,真))\r
$('E')。=的textContent JSON.stringify(chunkify(A,N,FALSE))\r
}\r
\r
$('S')的addEventListener('输入',钙);\r
$('N')的addEventListener('输入',钙)。\r
计算();\r
}
\r
< P>切片<输入类型=数字值=20 ID =的>物品进入\r
<输入类型=数字值=6ID =N>块:或其可/差异无显着\r
< pre ID =B>< / pre>\r
< pre ID =E>< / pre>
\r
Imagine I have an JS array like this:
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
What I want is to split that array into N smaller arrays. For instance:
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]]
For Python, I have this:
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
For JS, the best right solution that I could come up with is a recursive function, but I don't like it because it's complicated and ugly. This inner function returns an array like this [1, 2, 3, null, 4, 5, 6, null, 7, 8], and then I have to loop it again and split it manually. (My first attempt was returning this: [1, 2, 3, [4, 5, 6, [7, 8, 9]]], and I decided to do it with the null separator).
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));
}
Here's a jsfiddle of that: http://jsfiddle.net/uduhH/
How would you do that? Thanks!
You can make the slices "balanced" (subarrays' lengths differ as less as possible) or "even" (all subarrays but the last have the same length):
function chunkify(a, n, balanced) {
if (n < 2)
return [a];
var len = a.length,
out = [],
i = 0,
size;
if (len % n === 0) {
size = Math.floor(len / n);
while (i < len) {
out.push(a.slice(i, i += size));
}
}
else if (balanced) {
while (i < len) {
size = Math.ceil((len - i) / n--);
out.push(a.slice(i, i += size));
}
}
else {
n--;
size = Math.floor(len / n);
if (len % size === 0)
size--;
while (i < size * n) {
out.push(a.slice(i, i += size));
}
out.push(a.slice(size * n));
}
return out;
}
///////////////////////
onload = function () {
function $(x) {
return document.getElementById(x);
}
function calc() {
var s = +$('s').value, a = [];
while (s--)
a.unshift(s);
var n = +$('n').value;
$('b').textContent = JSON.stringify(chunkify(a, n, true))
$('e').textContent = JSON.stringify(chunkify(a, n, false))
}
$('s').addEventListener('input', calc);
$('n').addEventListener('input', calc);
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>
这篇关于拆分JS数组为N阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!