Advertisement
Guest User

Untitled

a guest
Sep 11th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. /**
  2. * Returns an array with arrays of the given size.
  3. *
  4. * @param myArray {Array} Array to split
  5. * @param chunkSize {Integer} Size of every group
  6. */
  7. function chunkArray(myArray, chunk_size){
  8. var results = [];
  9.  
  10. while (myArray.length) {
  11. results.push(myArray.splice(0, chunk_size));
  12. }
  13.  
  14. return results;
  15. }
  16.  
  17. // Split in group of 3 items
  18. var result = chunkArray([1,2,3,4,5,6,7,8], 3);
  19. // Outputs : [ [1,2,3] , [4,5,6] ,[7,8] ]
  20. console.log(result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement