Skip to content
Subhajit Sahu edited this page Feb 4, 2021 · 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 lists = require('extra-lists');

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

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

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


References

Clone this wiki locally