Guest User

Untitled

a guest
Dec 11th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.37 KB | None | 0 0
  1. 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]], [[]]);
  2.  
  3. const a = [1,2,3,4,5,6,7,8,9]
  4.  
  5. console.log(chunk(a, 2)); // -> [[1, 2], [3, 4], [5, 6], [7, 8], [9]]
  6. console.log(chunk(a, 3)); // -> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  7. console.log(chunk(a, 4)); // -> [[1, 2, 3, 4], [5, 6, 7, 8], [9]]
Add Comment
Please, Sign In to add comment