JavaScript - Write a function that splits an array and returns them as a two-dimensional array



Question - Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.


Solution Basic approach


console = { 
    log: print,
    warn: print,
    error: print
};
function chunkArrayInGroups(arr, size) {
    let newArr = []
    while(arr.length){
        newArr.push(arr.splice(0, size));
    }
    return newArr;
}
console.log(chunkArrayInGroups(["a", "b", "c", "d", "e", "f"], 2));

Output:


a,b,c,d,e,f