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 _.forEachRight() Method
Next article icon

Lodash _.forEach() Method

Last Updated : 03 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Lodash _.forEach() method iterates over elements of the collection and invokes iterate for each element.

Syntax:

_.forEach(collection, [iteratee = _.identity]);

Parameters:

  • collection: This parameter holds the collection to iterate over.
  • iteratee: It is the function that is invoked per iteration.

Return Value:

This method returns the collection. 

Example 1: In this example, we are iterating an array by the use of the _.forEach() method and printing the results in the console.

JavaScript
// Requiring the lodash library   const _ = require("lodash");  // Use of _.forEach() method  _.forEach(['c', 'cpp', 'java', 'python'], function (value) {     console.log(value); }); 

Output:

'c'
'cpp'
'java'
'python'

Example 2: In this example, we are iterating an object by the use of the _.forEach() method and printing the results in the console.

JavaScript
// Requiring the lodash library   const _ = require("lodash");  // Use of _.forEach() method  _.forEach({ 'x': 1, 'y': 2 }, function (value, key) {     console.log(key); }); 

Output:

'x'
'y'

Note: This will not work in normal JavaScript because it requires the library lodash to be installed. 


Next Article
Lodash _.forEachRight() Method

S

sanjoy_62
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Lodash

Similar Reads

    Lodash _.countBy() Method
    Lodash _.countBy method creates an object composed of keys generated from the results of running each element of collection through iteratee. The corresponding value of each key is the number of times the key was returned by iterate. Syntax:_.countBy(collection, [iteratee=_.identity])Parameters: Thi
    3 min read
    Lodash _.every() Method
    Lodash _.every() method checks if the predicate returns true for all elements of the collection and iteration is stopped once the predicate returns falsely. Note: Also this method returns true for empty collections because everything is true for elements of empty collections. Syntax_.every(collectio
    3 min read
    Lodash _.filter() Method
    The Lodash _.filter() method iterates over a collection (array or object) and returns a new array of elements that meet a specified condition (predicate), enabling efficient data filtering and extraction.Note: This method is not similar to the _.remove() method as this method returns a new array. Sy
    3 min read
    Lodash _.find() Method
    The Lodash _.find() method searches through a collection (array or object) and returns the first element that satisfies a specified condition (predicate). It’s useful for quickly locating a matching item within a collection. If no match is found, it returns undefined.Syntax_.find(collection, predica
    2 min read
    Lodash _.findLast() Method
    Lodash _.findLast() method iterates over the elements in a collection from the right to the left. It is almost the same as _.find() method. Syntax:_.findLast(collection, predicate, fromIndex);Parameters:collection: It is the collection that the method iterates over.predicate: It is the function that
    3 min read
    Lodash _.flatMap() Method
    Lodash _.flatMap() method creates a flattened array of values by running each element in the collection through iterate and flattening the mapped results. Syntax:_.flatMap(collection, iteratee);Parameters: collection: This parameter holds the collection to iterate over.iterate: This parameter holds
    3 min read
    Lodash _.flatMapDeep() Method
    Lodash _.flatMapDeep() method creates a flattened array of values by running each element in the given collection through the iteratee function and recursively flattens the mapped results. It is similar to the _.flatMap() method. Syntax:_.flatMapDeep(collection, iteratee);Parameters:collection: It i
    2 min read
    Lodash _.flatMapDepth() Method
    Lodash's _.flatMapDepth() method flattens an array by applying a function to each element and recursively flattening the results up to a specified depth. It's similar to _.flatMap() but allows control over the flattening depth.Syntax:_.flatMapDepth( collection, iteratee, depth )Parameters: This meth
    2 min read
    Lodash _.forEach() Method
    Lodash _.forEach() method iterates over elements of the collection and invokes iterate for each element.Syntax:_.forEach(collection, [iteratee = _.identity]);Parameters:collection: This parameter holds the collection to iterate over.iteratee: It is the function that is invoked per iteration.Return V
    2 min read
    Lodash _.forEachRight() Method
    Lodash _.forEachRight() method iterates over elements of collection from right to left. It is almost the same as the _.forEach() method.Syntax:_.forEachRight( collection, iteratee );Parameters: collection: This parameter holds the collection to iterate over.iteratee: It is the function that is invok
    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