Skip to content

Instantly share code, notes, and snippets.

@arjunasuresh3
Forked from dannycroft/chunk.lodash.js
Last active August 29, 2015 14:20
Show Gist options
  • Save arjunasuresh3/c9a4cb08b11a13cbc937 to your computer and use it in GitHub Desktop.
Save arjunasuresh3/c9a4cb08b11a13cbc937 to your computer and use it in GitHub Desktop.
/**
* Lodash / Underscore method for breaking data sets into smaller sets (chunks)
*
* @arguments
*
* collection: Array / Object
* chuckSize: Number
*
* @example
*
* -> _.chunk(["number","array_element","city_prefix","city_suffix","street_suffix"], 3));
* -> [["number","array_element","city_prefix"], ["city_suffix","street_suffix"]]
*/
_.mixin({
'chunk': function (collection, chunkSize) {
if (!collection || _.isNaN(parseInt(chunkSize, 10))) { return [];}
return _.toArray(_.groupBy(collection, function (iterator, index) {
return Math.floor(index / parseInt(chunkSize, 10));
}));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment