Divides an array into several chunks of size.
size
Time complexity: O(n)
Space complexity: O(n)
The array to chunk
The size of each chunk
The new array containing chunks of the original array
array
Array
chunk([1, 2, 3, 4, 5, 6], 2); // [[1, 2], [3, 4], [5, 6]] Copy
chunk([1, 2, 3, 4, 5, 6], 2); // [[1, 2], [3, 4], [5, 6]]
chunk([1, 2, 3, 4, 5, 6], 5); // [[1, 2, 3, 4, 5], [6]] Copy
chunk([1, 2, 3, 4, 5, 6], 5); // [[1, 2, 3, 4, 5], [6]]
chunk([1, 2, 3, 4, 5, 6], 6); // [[1], [2], [3], [4], [5], [6]] Copy
chunk([1, 2, 3, 4, 5, 6], 6); // [[1], [2], [3], [4], [5], [6]]
chunk([1, 2, 3, 4, 5, 6], 100); // [[1, 2, 3, 4, 5, 6]] Copy
chunk([1, 2, 3, 4, 5, 6], 100); // [[1, 2, 3, 4, 5, 6]]
Divides an iterable into several chunks of size.
The iterable to chunk
An iterator yieliding new arrays containing chunks of the original iterable
iterable
Iterable
[...chunk(new Set([1, 2, 3, 4, 5, 6]), 2)]; // [[1, 2], [3, 4], [5, 6]] Copy
[...chunk(new Set([1, 2, 3, 4, 5, 6]), 2)]; // [[1, 2], [3, 4], [5, 6]]
[...chunk(new Set([1, 2, 3, 4, 5, 6]), 5)]; // [[1, 2, 3, 4, 5], [6]] Copy
[...chunk(new Set([1, 2, 3, 4, 5, 6]), 5)]; // [[1, 2, 3, 4, 5], [6]]
[...chunk(new Set([1, 2, 3, 4, 5, 6]), 6)]; // [[1], [2], [3], [4], [5], [6]] Copy
[...chunk(new Set([1, 2, 3, 4, 5, 6]), 6)]; // [[1], [2], [3], [4], [5], [6]]
[...chunk(new Set([1, 2, 3, 4, 5, 6]), 100)]; // [[1, 2, 3, 4, 5, 6]] Copy
[...chunk(new Set([1, 2, 3, 4, 5, 6]), 100)]; // [[1, 2, 3, 4, 5, 6]]
Divides an array into several chunks of
size
.Time complexity: O(n)
Space complexity: O(n)