假设我有数组:
['a','b','c','d','e','f','g','h','i','j']
如果
,我想将上面的数组分成N个较小的均匀块数组N = 2 => ['a', 'c', 'e', 'f', 'h', 'j'], ['b', 'd', 'f', 'g', 'i']
N = 3 => ['a', 'd', 'g', 'j'], ['b', 'e', 'h'], ['c', 'f', 'i']
N = 4 => ['a', 'e', 'i'], ['b', 'f', 'j'], ['c', 'g'], ['d', 'h']
我怎样才能实现这一目标并获得超过0(n)的复杂性?
答案 0 :(得分:2)
O(N):
const input = ['a','b','c','d','e','f','g','h','i','j'];
const n = 3;
const result = input.reduce((result, item, itemIndex) => {
result[itemIndex % n] = (result[itemIndex % n] || []).concat(item);
return result;
}, []);
console.log(result);
答案 1 :(得分:2)
我相信Dan O在此之后:
const input = ['a','b','c','d','e','f','g','h','i','j'];
const n = 3;
const result = input.reduce((result, item, itemIndex) => {
result[itemIndex % n] = (result[itemIndex % n] || []).concat(item);
return result;
}, []);
console.log(result);

但更有效的一个 - O(n)将是:
const input = ['a','b','c','d','e','f','g','h','i','j'];
const n = 3;
const result = input.reduce((result, item, itemIndex) => {
var idx = itemIndex % n;
if (!result[idx])
result[idx] = [];
result[idx].push(item);
return result;
}, []);
console.log(result);

答案 2 :(得分:1)
您可以创建一个函数,将原始数组和所需的块数作为参数。成功的关键是使用modulo
来确定returnArray的哪个索引放置元素。
var origArr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
function evenChunk(chunks, inputArr) {
var returnArray = [];
// cycle through out inputArr progressively filling each returnArray array
for (var x = 0, len = inputArr.length; x < len; x++) {
var selectedIndex = x % chunks; // find the expected index
// if the index is undefined, instantiate it
if (!returnArray[selectedIndex]){
returnArray[selectedIndex] = [];
}
// push the array element at the modulo position of x onto the selectedIndex array
returnArray[selectedIndex].push(inputArr[x]);
}
return returnArray;
}
console.log(evenChunk(2, origArr));
console.log(evenChunk(3, origArr));
console.log(evenChunk(4, origArr));
.as-console-wrapper{max-height: 100% !important; top: 0;}
答案 3 :(得分:0)
试试这个:
var test = ['a','b','c','d','e','f','g','h','i','j'];
function chunkArray(array, n) {
if(n === 1) return array;
return array.reduce(function(result, item, index) {
result[index%n] = (result[index%n] || []).concat(item);
return result;
}, []);
}
console.log(chunkArray(test, 1));
console.log(chunkArray(test, 2));
console.log(chunkArray(test, 3));
console.log(chunkArray(test, 4));
console.log(chunkArray(test, 5));
答案 4 :(得分:0)
以下是我对此问题的看法。这是一个顺序袋填充物。如果你想放入一个函数,前两行是参数,最后一行是return。
var a = ['a','b','c','d','e','f','g','h','i','j'];
var n = 3; // n is number of blocks
var l = a.length; var v = Math.floor(l/n);
var c = 0; var i = -1; var r = []; var p = [];
while(true){
i=i+1; if (i==l) {
r[r.length-1] = r[r.length-1].concat(p);
break;
}
p.push(a[i]);
c=c+1;
if (c==v){
r = r.concat([p]);
p=[]; c=0;
}
}
console.log(r); // r is the result
&#13;
答案 5 :(得分:0)
var arr = ['a','b','c','d','e','f','g','h','i','j'];
function split(arr, N) {
var counter = 1;
return arr.reduce(function(obj, item) {
if(counter > N) counter = 1;
if(!obj[counter]) obj[counter] = [];
obj[counter].push(item);
counter++
return obj;
}, [])
};
var result = split(arr, 4);
console.log(result);
&#13;
答案 6 :(得分:0)
你可以尝试这样的东西,它只是一个接受要分割的数组的函数,并返回一个均匀分布的数组数组,而不必手动设置元素数。
msgbox