SHARE
TWEET

Untitled

a guest Sep 11th, 2019 105 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top