Partitioning in JavaScript

javascript, list

Solution

If using Underscore.js, you can implement this with `groupBy()` and `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.)

jsFiddle: http://jsfiddle.net/MW3BS/

Problem

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.

Original source

Related problems