Skip to content

Instantly share code, notes, and snippets.

@auser
Created November 19, 2016 21:18
Show Gist options
  • Save auser/ce986b1805f78395ad42b485ad895864 to your computer and use it in GitHub Desktop.
Save auser/ce986b1805f78395ad42b485ad895864 to your computer and use it in GitHub Desktop.
/**
* Small helper to split an array into groups of various size
*
* Usage:
* group([1, 2, 3, 4, 5], 2) # -> [[1, 2], [3, 4], [5]]
* group([1, 2, 3, 4], 5) # -> [[1, 2, 3, 4]]
**/
let group = (arr, numPerGroup) => {
if (arr.length <= 0) return [];
let groups = [];
let numgroups = (arr.length/(arr.length/numPerGroup));
while(arr.length) {
groups.push(arr.splice(0, numPerGroup))
};
return groups;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment