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

Solution not working or have any suggestions? Please send an email to [email protected]



Join Our Facebook Group


Share this example


Download Android App

download android app

Popular javascript Examples









codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube