Skip to content
Subhajit Sahu edited this page Jun 18, 2020 · 13 revisions

Breaks lists into chunks of given size. 🏃 📼 📦 🌔 📒

lists.chunk(x, [n], [s]);
// x: lists
// n: chunk size (1)
// s: chunk step (n)
const entries = require('extra-entries');

var x = [
  ['a', 1], ['b', 2], ['c', 3], ['d', 4],
  ['e', 5], ['f', 6], ['g', 7], ['h', 8]
];
entries.chunk(x, 3).map(x => [...x]);
// [
//   [ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ] ],
//   [ [ 'd', 4 ], [ 'e', 5 ], [ 'f', 6 ] ],
//   [ [ 'g', 7 ], [ 'h', 8 ] ]
// ]

entries.chunk(x, 2, 3).map(x => [...x]);
// [
//   [ [ 'a', 1 ], [ 'b', 2 ] ],
//   [ [ 'd', 4 ], [ 'e', 5 ] ],
//   [ [ 'g', 7 ], [ 'h', 8 ] ]
// ]

entries.chunk(x, 4, 3).map(x => [...x]);
// [
//   [ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ], [ 'd', 4 ] ],
//   [ [ 'd', 4 ], [ 'e', 5 ], [ 'f', 6 ], [ 'g', 7 ] ],
//   [ [ 'g', 7 ], [ 'h', 8 ] ]
// ]

references

Clone this wiki locally