Last active
August 29, 2015 14:06
-
-
Save fabiomontefuscolo/71d4ccbd7af58cad8f78 to your computer and use it in GitHub Desktop.
Groups elements of array
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
// http://jsfiddle.net/montefuscolo/ogtzpt4t/ | |
Object.defineProperty( | |
Array.prototype, | |
'group', | |
{ | |
enumerable: false, | |
value: function (n) { | |
var initial = [ | |
[] | |
]; | |
function reducer(m, e) { | |
if (m[m.length - 1].length < n) { | |
m[m.length - 1].push(e); | |
} else { | |
m.push([e]); | |
} | |
return m; | |
} | |
return this.reduce(reducer, initial); | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment