Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const chunk = (arr, size) => arr.reduce((a, n) => a[a.length-1].length < size ? [...a.slice(0,-1), [...a[a.length-1], n]] : [...a, [n]], [[]]);
- const a = [1,2,3,4,5,6,7,8,9]
- console.log(chunk(a, 2)); // -> [[1, 2], [3, 4], [5, 6], [7, 8], [9]]
- console.log(chunk(a, 3)); // -> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
- console.log(chunk(a, 4)); // -> [[1, 2, 3, 4], [5, 6, 7, 8], [9]]
Add Comment
Please, Sign In to add comment