I'm a Floridian web engineer who planted his roots in Paris. Some of my favorite past-times are analyzing and optimizing development processes, building solid APIs, mentoring junior devs, and long walks on the beach.

Javascript Should Have a groups_of Function

I forget why, but there was a certain point in time where I thought that javascript should have a getgroupsof function. So here is a quick and easy implementation that will roll through an array and grab elements in groups of n. Anywho, now you can enjoy countless hours of grabbing things, n at a time, yippeee!

function get_group_of(limit,array){
    group = Array();
    while(limit-- > 0){ 
        if(value = array.shift()){
            group.push(value);
        }
    }
    return group.length > 0? group: false;
}

stuff = new Array();
for(i=0; i<1000; i++){ stuff.push(i); }
final_group=Array();

while(group=get_group_of(3,stuff)){
    for(letter in group){
        final_group.push(letter);
    }
    final_group.push('===next group====');
}
console.log(final_group)