Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split an array into groups [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 551
500 Avatar asked Nov 20 '22 20:11

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 176
starbeamrainbowlabs Avatar answered Nov 29 '22 05:11

starbeamrainbowlabs