Skip to content

Instantly share code, notes, and snippets.

@spoike
Created June 27, 2013 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spoike/5876045 to your computer and use it in GitHub Desktop.
Save spoike/5876045 to your computer and use it in GitHub Desktop.
Paged collections in lodash
// Use _.mixin to extend lodash
_.mixin({
'paged': function(collection, size) {
var pages = [];
this.each(myarr, function(val, i) {
var pos = Math.floor(i/size);
if (!pages[pos]) pages.push([]);
pages[pos].push(val);
});
return pages;
}
});
// Test
var myarr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'];
console.log(_.paged(myarr, 4));
console.log(_(myarr).paged(6));
@spoike
Copy link
Author

spoike commented Jun 27, 2013

Test available at jsfiddle here: http://jsfiddle.net/spoike/NhSxW/

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