Skip to content

Instantly share code, notes, and snippets.

@DiegoSeC
Created July 11, 2013 23:07
Show Gist options
  • Save DiegoSeC/5980123 to your computer and use it in GitHub Desktop.
Save DiegoSeC/5980123 to your computer and use it in GitHub Desktop.
Split array in chunks using underscore.js
# Thanks to: http://stackoverflow.com/questions/8566667/split-javascript-array-in-chunks-using-underscore-js
_.mixin
chunk: (array, unit) ->
result = _.groupBy array, (element, index) ->
Math.floor index / unit
_.toArray result
data = [1,2,3,4,5,6,7,8,9]
# We pass the array and the size of the chunk
list = _.chunk data, 2
@tokland
Copy link

tokland commented Aug 2, 2017

AFICS this may not work on all implementations, a JS object has "an unordered collection of properties".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment