There are multiple to do this:
I. Normal function
function partition(items, n) {
var result = _.groupBy(items, function(item, i) {
return Math.floor(i % n);
});
return _.values(result);
}
myArray = [1,2,3,4,5,6,7,8,9,10];
partition(myArray, 3);
II. Adding a prototype method
Array.prototype.partition = function(n) {
var result = _.groupBy(this, function(item, i) {
return Math.floor(i % n);
});
return _.values(result);
}
myArray = [1,2,3,4,5,6,7,8,9,10];
myArray.partition(3)
Create an _.partitionToGroups method
_.partitionToGroups = function(items, n) {
var result = _.groupBy(items, function(item, i) {
return Math.floor(i % n);
});
return _.values(result);
}
myArray = [1,2,3,4,5,6,7,8,9,10];
_.partitionToGroups(myArray, 3);