-
-
Save arjunasuresh3/c9a4cb08b11a13cbc937 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* 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