javascript

Break an array into fixed length array groups using lodash in Javascript

You can use lodash method _.chunk() to split or break array elements into fixed sized arrays of array.

const arr = ['one', 'two', 'three', 'four', 'five'];
const arr_chunks = _.chunk(arr, 2);
Output
[["one","two"],["three","four"],["five"]]

You can check lodash chunk syntax as below

_.chunk(Array, size);

This takes two arguments Array and size:

Array: You can pass your array as this required parameter that you want to break into chunks.

size: This is an optional parameter which takes the integer type value and create you array of arrays with the size that you enter as this parameter. By deafult the value of size is one

Live Demo

Was this helpful?