Search code snippets, questions, articles...

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);
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

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

Search Index Data (The code snippet can also be found with below search text)

_.chunk() lodash
Was this helpful?