Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. // A function that splits an array (first argument) into groups the length of size (second argument).
  2. // Returns as a 2D array.
  3.  
  4. function chunkArrayInGroups(arr, size)
  5. {
  6. // An empty array to store our new 2D array.
  7. var TwoArray = [];
  8.  
  9. // Counting through loop where to slice index and stops when reaches arr.length. Then use push() to add slice to new array.
  10. for (var i = 0; i < arr.length; i += size)
  11. {
  12. TwoArray.push(arr.slice(i, i + size));
  13. }
  14. return TwoArray;
  15. }
  16.  
  17. console.log(chunkArrayInGroups(["a", "b", "c", "d"], 2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement