How to Split JavaScript Array in Chunks using Lodash?
Last Updated :
07 May, 2024
Splitting a JavaScript array into chunks consists of dividing the array into smaller subarrays based on a specified size or condition. In this article, we will explore different approaches to splitting JavaScript array into chunks using Lodash.
Below are the approaches to Split JavaScript array in chunks using Lodash:
Using Lodash _.chunk() Method
In this approach, we are using the _.chunk method from Lodash to split the array into chunks of size 3, and then we log the resulting array res to the console.
Syntax:
_.chunk(array, size);
Example: The below example uses _.chunk() Method to Split the JavaScript array into chunks using Lodash.
JavaScript
const _ = require('lodash');
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const res = _.chunk(array, 3);
console.log(res);
Output
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ], [ 10 ] ]
Using Lodash _.reduce() Method
In this approach, we are using _.reduce from Lodash to iterate over the array and accumulate chunks of size chunkSize by pushing elements into subarrays. Finally, we log the resulting chunked array res to the console.
Syntax:
_.reduce(collection, iteratee, accumulator)
Example: The below example uses the _.reduce() Method to Split the JavaScript array into chunks using Lodash.
JavaScript
const _ = require('lodash');
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const chunkSize = 3;
const res = _.reduce(array, (result, value, index) => {
if (index % chunkSize === 0) {
result.push([]);
}
result[Math.floor(index / chunkSize)].push(value);
return result;
}, []);
console.log(res);
Output
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ], [ 10 ] ]
Similar Reads
Split JavaScript Array in Chunks Using Lodash? When working with arrays in JavaScript, you may encounter situations where you need to split an array into smaller chunks. This can be particularly useful when dealing with large datasets or when performing operations in a more manageable and efficient manner. The Lodash library provides a convenien
2 min read
Split an Array into Chunks in JavaScript Here are different methods to split an array into chunks in JavaScript.1. Using slice() MethodThe array slice() method returns a new array containing the selected elements. This method selects the elements starting from the given start argument and ends at, but excluding the given end argument. Synt
3 min read
How to use 'lodash' Package for Array Manipulation in a JavaScript Project ? In this article, we'll look at how to use lodash for array manipulation tasks like sorting, filtering, mapping, etc. The 'lodash' is a very important and useful npm package that provides a set of functions to work with arrays, strings, objects, etc. Working with arrays is an important task in a java
4 min read
How to Import a Single Lodash Function in JavaScript ? In JavaScript, we have to import various functions for operation, but importing a single function is also important for optimizing code and reducing unnecessary dependencies. Below are the approaches to importing a single Lodash function in JavaScript: Table of Content Using ES6 import syntaxUsing C
2 min read
How to Paginate an Array in JavaScript? Pagination is a common requirement in web applications especially when dealing with large datasets. It involves dividing a dataset into smaller manageable chunks or pages. In JavaScript, we can paginate an array by splitting it into smaller arrays each representing a page of the data. Below are the
2 min read