The Wayback Machine - https://web.archive.org/web/20240121085557/https://www.geeksforgeeks.org/lodash-_-chunk-method/
Open In App
Related Articles

Lodash _.chunk() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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 :


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks 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!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 20 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials