Skip to content

Instantly share code, notes, and snippets.

@fxbeckers
Created April 29, 2013 12:45
Show Gist options
  • Save fxbeckers/5481379 to your computer and use it in GitHub Desktop.
Save fxbeckers/5481379 to your computer and use it in GitHub Desktop.
Split array into buckets of size n
var bucketize = (x: any[], nInBucket: number) => {
var i = 0.0;
var iX = _.map(x, (x) => { return { i: i++, x: x }; });
var groupedItems = _.groupBy(iX, (ixGrp) => { return Math.floor(ixGrp.i / nInBucket); });
return _.map(_.values(groupedItems), (group: any[]) => {
return _.pluck(group, 'x');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment