Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Lodash-Array
  • Lodash-Collection
  • Lodash-Function
  • Lodash-Lang
  • Lodash-Math
  • Lodash-Object
  • Lodash-Sequence
  • Lodash-String
  • Lodash-Util
  • JavaScript
  • Web Technology
Open In App
Next Article:
Lodash _.compact() Function
Next article icon

Lodash _.chunk() Method

Last Updated : 30 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
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 :


Next Article
Lodash _.compact() Function

T

tarun007
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Lodash
  • Lodash Array

Similar Reads

    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 al
    3 min read
    Lodash _.compact() Function
    Lodash _.compact() function is used to create an array with all false values removed in JavaScript. so it returns a new array of filtered values.Syntax:_.compact(array);Parameters:array: It is an array to be compacted.Note: The values are false, null, 0, "", undefined, and NaN are falsey.Return Valu
    2 min read
    Lodash _.concat() Function
    Lodash _.concat() function is used to concatenate the arrays in JavaScript. and it returns the new concatenated array.Syntax:_.concat(array, [values]);Parameters:array: It is an array to which values are to be added.values: It is the Array of values that is to be added to the original array.Note: Th
    3 min read
    Lodash _.difference() Function
    Lodash _.difference() function is used to remove a single element or the array of elements from the original array. This function works pretty much the same as the core function of JavaScript i.e. filter.Syntax:_.difference(array, [values]);Parameters:array: It is the array from which different elem
    2 min read
    Lodash _.differenceBy() Method
    The Lodash _.differenceBy() method is used to remove the values from the original array by iterating over each element in the array by using the Iterate function. It is almost the same as _.difference() function.Syntaxlodash.differenceBy(array, [values], [iterate=_.identity])ParametersThis function
    3 min read
    Lodash _.differenceWith() Method
    Lodash _.differenceWith() method is similar to the _.difference() method that returns the array containing the values that are in the first array not in the second array but in _.differenceWith() all the elements of the first array are compared with the second array by applying comparison provided i
    3 min read
    Lodash _.drop() Method
    Lodash _.drop() method is used to drop the elements in a given array.Syntax:_.drop(array, number);Parameters:Array: It is the original array from which the elements are to be removed.Number: It is the number of elements to be removed from the array.Note: The elements are removed from the index 0 of
    2 min read
    Lodash _.dropRight() Function
    Lodash _.dropRight() function is used to delete the elements from the right of the array i.e from the (n-1)th element.Syntax: _.dropRight(array, n);Parameters:array: It is the original array from which elements are to be deleted.n: Here n is the number of elements that are to be deleted from the arr
    3 min read
    Lodash _.dropRightWhile() Function
    Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The _.dropRightWhile() function is used to delete the slice of the array excluding elements dropped from the end until the predicate function returns false.Synt
    2 min read
    Lodash _.dropWhile() Method
    Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The Loadsh.dropWhile() method is used to return the slice of the given array. This takes a predicate function that iterates through each element of the array an
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences