Wednesday, September 3, 2025
HomeLanguagesJavascriptLodash _.chunk() Method

Lodash _.chunk() Method

Lodash _.chunk() function is used to break the array into small chunks. Each chunk is an array of sizes as given.

Syntax:

_.chunk(array, size);

Parameters:

  • array: An array to be processed by chunk function.
  • size: This describes the size of the chunk.

Return Value:

It returns the array of chunks that is also an array.

Example 1: In this example, we are breaking the array by passing size ‘1’ into the _.chunk() method.

Javascript




// Requiring the lodash module
// in the script
const _ = require("lodash");
let arr = [1, 2, 3, 4, 5, 6];
 
// Making chunks of size 1
console.log(_.chunk(arr, 1))


Output: 

OUTPUT EXAMPLE 1

Example 2: In this example, we are breaking the array by passing size ‘3’ into the _.chunk() method. The size of chunk can be varied and array of different data type can be used with chunk function.

Javascript




// Requiring the lodash module
// in the script
let _ = require("lodash");
let arr = [1, 2, 3, 4, 5, 6,
    "a", "b", "c", "d"];
console.log("Before: ", arr)
 
// Making chunks of size 3
console.log("After: ", _.chunk(arr, 3))


Output: 

Example 3: In this example, we are breaking the 2d array by passing size ‘2’ into the _.chunk() method. Using array of array with chunk.

Javascript




// Requiring the lodash module
// in the script.
let lodash = require("lodash");
let arr = [
    [1, 2, 3],
    [4, 5, 6, 7, 8],
    [9, 10, 1, 2]
];
 
console.log("Before: ", arr)
console.log("After: ", lodash.chunk(arr, 2))


Output: 

Example 4: In this example, we are breaking the array of object by passing size ‘1’ into the _.chunk() method.

Javascript




let lodash = require("lodash");
let arr = [
    { "a": 1, "b": 2, "c": 3 },
    { "d": 1, "e": 2, "f": 3 },
    { "d": 1, "e": 2, "f": 3 }
];
 
// Array before breaking in to chunks
console.log("Before: ", arr)
 
// Printing the first element
// of the chunk as size 1
console.log("After: ",
    lodash.chunk(arr, 1)[0]);


Output :

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6746 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS