A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/lodash-_-foreach-method/ below:

Lodash _.forEach() Method - GeeksforGeeks

Lodash _.forEach() Method

Last Updated : 03 Sep, 2024

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

Syntax:
_.forEach(collection, [iteratee = _.identity]);
Parameters: 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. 



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4