-
Notifications
You must be signed in to change notification settings - Fork 1
chunk
Subhajit Sahu edited this page Dec 5, 2022
·
13 revisions
Break map into chunks of given size.
function chunk(x, n, s)
// x: a map
// n: chunk size [1]
// s: chunk step [n]
const map = require('extra-map');
var x = new Map([
['a', 1], ['b', 2], ['c', 3], ['d', 4],
['e', 5], ['f', 6], ['g', 7], ['h', 8]
]);
map.chunk(x, 3);
// → [
// → Map(3) { 'a' => 1, 'b' => 2, 'c' => 3 },
// → Map(3) { 'd' => 4, 'e' => 5, 'f' => 6 },
// → Map(2) { 'g' => 7, 'h' => 8 }
// → ]
map.chunk(x, 2, 3);
// → [
// → Map(2) { 'a' => 1, 'b' => 2 },
// → Map(2) { 'd' => 4, 'e' => 5 },
// → Map(2) { 'g' => 7, 'h' => 8 }
// → ]
map.chunk(x, 4, 3);
// → [
// → Map(4) { 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 },
// → Map(4) { 'd' => 4, 'e' => 5, 'f' => 6, 'g' => 7 },
// → Map(2) { 'g' => 7, 'h' => 8 }
// → ]