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?
Similar Posts
- Create fixed length array with blank values in it Javascript
- Lodash - Flatten an array to a fixed depth
- Merge multiple arrays and strings into single array using Lodash
- Create groups or categorize the axis points of highcharts.js
- Concatenate multiple objects into one using Javascript
- Combine or merge two or multiple arrays into single array in Javascript
- Javascript to split string into array of characters