DavidSzczesniak
10/28/2017 - 9:08 PM

Chunk Arrays

Splits array given(first argument), into 2 separate arrays inside one multi-dimensional array. In 2 groups the size of the second argument given.

function chunkArrayInGroups(arr, size) {
  // Make empty array
  var holderArray = [];
  // Always starting at 0 when adding to it
  var count = 0;
   while (count < arr.length) {
     // Push given array to new empty array, sliced up depending on size given
     holderArray.push(arr.slice(count, count + size));
     // Reset count else infite loop will occour
     count = count + size;
   }
  
  return holderArray;
}

// Example call
chunkArrayInGroups(["a", "b", "c", "d"], 2);