Created
November 19, 2016 21:18
-
-
Save auser/ce986b1805f78395ad42b485ad895864 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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