Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partitioning in JavaScript [duplicate]

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.

like image 747
500 Avatar asked Jul 05 '12 13:07

500


1 Answers

I think you will have to use a for loop, don't know of any inbuilt functions...

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;
}
like image 65
starbeamrainbowlabs Avatar answered Oct 23 '22 02:10

starbeamrainbowlabs