Syntax, Parameter and Return Value of Lodash _.chunk() Method
The syntax of the _.chunk() method in Lodash is straightforward yet flexible, catering to various use cases. Here's a general overview of its structure:
Syntax
Let’s see the syntax of using the _.chunk() method.
_.chunk(array, [size=1])
Parameters
array (Array): The array to process.
[size=1] (number): The length of each chunk. This is an optional parameter, with a default value of 1, meaning that if no size is specified, the method will create chunks containing one element each.
Return Value
Returns (Array): Returns a new array comprising chunks (sub-arrays) of the original array.
The _.chunk() method does not modify the original array; instead, it creates a new array with the sub-arrays. The size of the last chunk may be less than the specified size if the array can't be divided evenly.
Examples of Lodash _.chunk() Method
Example 1: Basic Chunking
JavaScript
const _ = require('lodash');
// Original array
const originalArray = [1, 2, 3, 4, 5, 6, 7];
// Chunking array into smaller arrays of size 2
const chunkedArray = _.chunk(originalArray, 2);
console.log(chunkedArray);
You can also try this code with Online Javascript Compiler
Run Code
Output:
[[1, 2], [3, 4], [5, 6], [7]]
This example demonstrates basic usage, where the array is split into chunks of size 2.
Example 2: Default Size Chunking
JavaScript
const chunkedArrayDefault = _.chunk(originalArray);
console.log(chunkedArrayDefault);
You can also try this code with Online Javascript Compiler
Run Code
Output:
[[1], [2], [3], [4], [5], [6], [7]]
Without specifying a size, each chunk contains only one element by default.
Example 3: Chunking with Larger Size
JavaScript
const largerChunkedArray = _.chunk(originalArray, 4);
console.log(largerChunkedArray);
You can also try this code with Online Javascript Compiler
Run Code
Output:
[[1, 2, 3, 4], [5, 6, 7]]
Here, the array is split into chunks of size 4, illustrating flexibility in chunk sizes.
Example 4: Chunking an Empty Array
JavaScript
const emptyArray = [];
const chunkedEmpty = _.chunk(emptyArray, 2);
console.log(chunkedEmpty);
You can also try this code with Online Javascript Compiler
Run Code
Output:
[]
Chunking an empty array returns an empty array, showcasing the method's robustness.
Frequently Asked Questions
What happens if the array size is not a multiple of the chunk size?
If the array size is not a multiple of the chunk size, the last chunk will contain fewer elements than the specified size.
Does _.chunk() modify the original array?
No, _.chunk() does not modify the original array. It returns a new array with the chunked data.
Can _.chunk() be used with arrays of objects?
Yes, _.chunk() works with any type of array elements, including objects, strings, and numbers.
Conclusion
The _.chunk() method in Lodash is an efficient and straightforward way to split arrays into smaller chunks. Its versatility and ease of use make it a valuable tool for managing large datasets, improving performance, and enhancing user experience in JavaScript applications. Whether for data processing or UI management, _.chunk() is a utility function that significantly simplifies array manipulation.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.