Skip to content

Instantly share code, notes, and snippets.

@fabiomontefuscolo
Last active August 29, 2015 14:06
Show Gist options
  • Save fabiomontefuscolo/71d4ccbd7af58cad8f78 to your computer and use it in GitHub Desktop.
Save fabiomontefuscolo/71d4ccbd7af58cad8f78 to your computer and use it in GitHub Desktop.
Groups elements of array
// 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