Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // A function that splits an array (first argument) into groups the length of size (second argument).
- // Returns as a 2D array.
- function chunkArrayInGroups(arr, size)
- {
- // An empty array to store our new 2D array.
- var TwoArray = [];
- // Counting through loop where to slice index and stops when reaches arr.length. Then use push() to add slice to new array.
- for (var i = 0; i < arr.length; i += size)
- {
- TwoArray.push(arr.slice(i, i + size));
- }
- return TwoArray;
- }
- console.log(chunkArrayInGroups(["a", "b", "c", "d"], 2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement