1const chunk = (arr, size) => arr.reduce((acc, e, i) => (i % size ? acc[acc.length - 1].push(e) : acc.push([e]), acc), []);
2
3// Examples
4chunk([1, 2, 3, 4, 5, 6, 7, 8], 3); // [[1, 2, 3], [4, 5, 6], [7, 8]]
5chunk([1, 2, 3, 4, 5, 6, 7, 8], 4); // [[1, 2, 3, 4], [5, 6, 7, 8]]
1const chunkSize = 10;
2const arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17];
3const groups = arr.map((e, i) => {
4 return i % chunkSize === 0 ? arr.slice(i, i + chunkSize) : null;
5}).filter(e => { return e; });
6console.log({arr, groups})
1function splitArrayIntoChunksOfLen(arr, len) {
2 var chunks = [], i = 0, n = arr.length;
3 while (i < n) {
4 chunks.push(arr.slice(i, i += len));
5 }
6 return chunks;
7}
8var alphabet=['a','b','c','d','e','f'];
9var alphabetPairs=splitArrayIntoChunksOfLen(alphabet,2); //split into chunks of two
1let input = [1,2,3,4,5,6,7,8,9];
2let chunked = []
3let size = 2;
4
5for (let i = 0; i < input.length; i += size) {
6 chunked.push(input.slice(i, i + size))
7}
8console.log(chunked)
1Array.prototype.chunk = function(size) {
2 let result = [];
3
4 while(this.length) {
5 result.push(this.splice(0, size));
6 }
7
8 return result;
9}
10
11const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
12console.log(arr.chunk(2));
1let input = [1,2,3,4,5,6,7,8];
2let chunked = []
3let size = 2;
4
5Array.from({length: Math.ceil(input.length / size)}, (val, i) => {
6 chunked.push(input.slice(i * size, i * size + size))
7})
8
9console.log(chunked);