Created
June 27, 2013 12:31
-
-
Save spoike/5876045 to your computer and use it in GitHub Desktop.
Paged collections in lodash
This file contains 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
// 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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test available at jsfiddle here: http://jsfiddle.net/spoike/NhSxW/