Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Returns an array with arrays of the given size.
- *
- * @param myArray {Array} Array to split
- * @param chunkSize {Integer} Size of every group
- */
- function chunkArray(myArray, chunk_size){
- var results = [];
- while (myArray.length) {
- results.push(myArray.splice(0, chunk_size));
- }
- return results;
- }
- // Split in group of 3 items
- var result = chunkArray([1,2,3,4,5,6,7,8], 3);
- // Outputs : [ [1,2,3] , [4,5,6] ,[7,8] ]
- console.log(result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement