JavaScript 中的分区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11345296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Partitioning in JavaScript
提问by 500
Please consider an array such as :
请考虑一个数组,例如:
arrayAll = [1,2,3,4,5,6,7,8,9]
Is there a package that enable to do partitioning to obtain :
是否有一个包可以进行分区以获得:
arrayALLPartionned = [[1,2,3],[4,5,6],[7,8,9]]
I can see how to do this with a for loop but would appreciate a "pre-made" function if existing.
我可以看到如何使用 for 循环执行此操作,但如果存在“预制”函数,我将不胜感激。
采纳答案by millimoose
If using Underscore.js, you can implement this with groupBy()
and values()
如果使用Underscore.js,您可以使用groupBy()
和values()
function partition(items, size) {
var result = _.groupBy(items, function(item, i) {
return Math.floor(i/size);
});
return _.values(result);
}
(This is less ugly in CoffeeScript.)
(这在 CoffeeScript 中不那么难看。)
jsFiddle: http://jsfiddle.net/MW3BS/
jsFiddle:http: //jsfiddle.net/MW3BS/
回答by starbeamrainbowlabs
I think you will have to use a for loop, don't know of any inbuilt functions...
我认为你将不得不使用 for 循环,不知道任何内置函数......
Try this function:
试试这个功能:
function splitarray(input, spacing)
{
var output = [];
for (var i = 0; i < input.length; i += spacing)
{
output[output.length] = input.slice(i, i + spacing);
}
return output;
}
回答by millimoose
Here's a recursive solution:
这是一个递归解决方案:
function partition(array, n) {
return array.length ? [array.splice(0, n)].concat(partition(array, n)) : [];
}
This takes advantage of the fact that Array#splice
destructively remove the specified items, and returns them as the function value. Note that this will destroy the input array, leaving it empty.
这利用了Array#splice
破坏性删除指定项目的事实,并将它们作为函数值返回。请注意,这将破坏输入数组,使其为空。
回答by Ross
I've added this solution to @dystroy's jspref hereand it appearsto run twice as fast as the other solutions. Edit: in Safari & Chrome but not Firefox
我已将此解决方案添加到@dystroy 的 jspref 中,它的运行速度似乎是其他解决方案的两倍。编辑:在 Safari 和 Chrome 但不是 Firefox
Here is functional style solution to add to the mix of answers here.
这是功能样式解决方案,可添加到此处的答案组合中。
It is a higher order function called toPartitions
which returns a callback for underscore's reduce method or the native array reduce method.
它是一个被调用的高阶函数toPartitions
,它返回下划线的 reduce 方法或本机数组 reduce 方法的回调。
Example usage:
用法示例:
[1,2,3,4,5,6,7,8,9].reduce( toPartitions( 3 ), [] );
The function:
功能:
function toPartitions ( size ) {
var partition = [];
return function ( acc, v ) {
partition.push( v );
if ( partition.length === size ) {
acc.push( partition );
partition = [];
}
return acc;
};
}
Like Clojure's partitionit will not include a tail partition when there are not enough elements.
与 Clojure 的分区一样,当元素不足时,它不会包含尾分区。
In your example you could do:
在您的示例中,您可以执行以下操作:
arrayALLPartionned = arrayAll.reduce( toPartitions( 3 ), [] ) );
If you don't want to use this with reduce
, but just have a function which takes an array and partition size you could do:
如果您不想将其与 一起使用reduce
,而只想使用一个接受数组和分区大小的函数,您可以执行以下操作:
function partition ( arr, size ) {
return arr.reduce( toPartitions( size ), [] );
}
Therefore the solution would just be:
因此,解决方案将是:
arrayALLPartionned = partition( arrayAll, 3 );
回答by Denys Séguret
One more solution, with no external library :
另一种解决方案,没有外部库:
function partition(items, size) {
var p = [];
for (var i=Math.floor(items.length/size); i-->0; ) {
p[i]=items.slice(i*size, (i+1)*size);
}
return p;
}
Demonstration : http://jsfiddle.net/dystroy/xtHXZ/
回答by Andy
Prototype has an array.partition function as well as an eachSlice() function. Sounds like eachSlice() is what you're looking for. If you're using jquery, there's a plug in to be able to use prototype functions. Here's a link to it... http://www.learningjquery.com/2009/02/implementing-prototypes-array-methods-in-jquery
Prototype 有一个 array.partition 函数以及一个 eachSlice() 函数。听起来 eachSlice() 就是你要找的。如果您使用 jquery,有一个插件可以使用原型函数。这是它的链接... http://www.learningjquery.com/2009/02/implementing-prototypes-array-methods-in-jquery
回答by Peter Olson
You can write your own prototype method to do this
您可以编写自己的原型方法来执行此操作
Array.prototype.partition = function(length) {
var result = [];
for(var i = 0; i < this.length; i++) {
if(i % length === 0) result.push([]);
result[result.length - 1].push(this[i]);
}
return result;
};
If you prefer not to add to the native prototype, you can write a simple function:
如果你不想添加到原生原型,你可以写一个简单的函数:
var partition = function(arr, length) {
var result = [];
for(var i = 0; i < arr.length; i++) {
if(i % length === 0) result.push([]);
result[result.length - 1].push(arr[i]);
}
return result;
};