The _.chunk()
method is used to split an array into smaller arrays of specified size. This method divides array into multiple smaller arrays (chunks). If the original array cannot be divided evenly, the last chunk will contain the remaining elements.
Syntax
_.chunk(array, size)
Parameters
- array (Required): The array to be divided into smaller arrays. This can be any valid array in JavaScript.
- size (Optional): The number of elements in each chunk. The default value is
1
. This can be any number that determines the size of the chunked arrays. If the size is greater than the array length, the entire array will be returned as a single chunk.
Return Value
The method returns a new array that contains multiple arrays (chunks), where each chunk contains size
elements (except the last chunk, which may contain fewer elements).
- If the
size
is larger than the length of the array, the entire array is returned as a single chunk. - If the
size
is less than or equal to the length of the array, the array is split accordingly.
Code Examples of Lodash _.chunk() Method
Let’s dive into some practical examples of how to use the _.chunk()
method.
1. Splitting an array of numbers into chunks of size 2.
const _ = require('lodash'); const arr = [1, 2, 3, 4, 5, 6]; const arrChunk = _.chunk(arr, 2); console.log(arrChunk); // Output: [[1, 2], [3, 4], [5, 6]]
2. Splitting the array of size 4.
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; const arrChunk = _.chunk(arr, 4); console.log(arrChunk); // Output: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10]]
3. Chunking with a Size Larger Than the Array
If the chunk size is greater than the length of the array, _.chunk()
will return an array containing the original array as a single chunk.
const arr = [1, 2, 3]; const arrChunk = _.chunk(arr, 5); console.log(arrChunk); // Output: [[1, 2, 3]]
4. Default Chunk Size
If no size is provided, the default size of 1
will be used, meaning each element will be in its own chunk.
const arr = [1, 2, 3, 4, 5]; const arrChunk = _.chunk(arr); console.log(arrChunk); // Output: [[1], [2], [3], [4], [5]]
5. Using _.chunk() method with Strings
The _.chunk()
method can be used with any array of elements, including strings.
const arr = ['a', 'b', 'c', 'd', 'e', 'f']; const arrChunk = _.chunk(arr, 2); console.log(arrChunk); // Output: [['a', 'b'], ['c', 'd'], ['e', 'f']]
6. Chunk an Empty Array
If the input array is empty, the result will also be an empty array.
const arr = []; const arrChunk = _.chunk(arr, 2); console.log(arrChunk); // Output: []
7. Split Nested Array into Chunks
const arr = [1, [2, 3], 4, [5, 6], 7]; const arrChunk = _.chunk(arr, 2); console.log(arrChunk); // Output: [[1, [2, 3]], [4, [5, 6]], [7]]
FAQs on Lodash _.chunk() Method
1. What happens if we don’t specify the size parameter?
If you don’t provide the size
parameter, the _.chunk()
method take default chunk size to 1
. This means each element in the array will be placed in its own individual chunk.
2. Can I chunk an array with a negative size?
No, the size
parameter must be a positive integer. A negative or zero value will not work and will throw an error.
3. Does _.chunk() method modify the original array?
No, _.chunk()
does not modify the original array. It creates a new array with the chunked elements, leaving the original array unchanged.
4. Can I chunk an array of objects or other complex data types?
Yes, _.chunk()
works with arrays of any data type, including primitive types (numbers, strings, etc.) and complex data types like objects or arrays.
5. Can _.chunk() method handle arrays of different data types?
Yes, the _.chunk()
method can handle arrays containing mixed data types. It works with any type of array elements, whether they are numbers, strings, objects, or a mix of different types.
6. How does _.chunk() method work with nested arrays?
The _.chunk()
method works with nested arrays by chunking the entire array, including subarrays. It does not flatten the array before chunking.