Created
January 31, 2016 16:25
-
-
Save impeto/8cb79e6bb0a0d9bb18ff to your computer and use it in GitHub Desktop.
Adds method `chunk` to the array class similar to Eloquent Collection's chunk method
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
(function() { | |
Array.prototype.chunk = function (chunkSize) { | |
var n = this.length; | |
if (chunkSize >= n) { | |
return [this]; | |
} | |
if (n == 0) { | |
return []; | |
} | |
var r = n % chunkSize; | |
var q = (n - r) / chunkSize; | |
var result = []; | |
for (var i = 0; i <= q; i++) { | |
result.push(this.slice(i * chunkSize, i * chunkSize + chunkSize)); | |
} | |
return result; | |
}; | |
})(); |
Thanks for sharing...
Thanks for the snippet.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This enables you to do things (in Vue Js) like