Open In App

Lodash _.forIn() Method

Last Updated : 30 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Lodash _.forIn() method Iterates over keys and values of the given object and invokes iteratee function for each property. The iteratee is invoked with three arguments: (value, key, object). Iteratee functions may exit iteration early by explicitly returning false.

Syntax:

_.forIn( object, iteratee_function);

Parameters:

  • object: This is the object to find in.
  • iteratee_function: the function that is invoked per iteration.

Return Value:

This method returns an object.

Example 1: In this example, we are printing the key of an object by the use of the lodash _.forIn() method.

javascript




// Defining Lodash variable
const _ = require('lodash');
 
let users = {
    'a': 1,
    'b': 2,
    'c': 3
};
 
_.forIn(users, function (value, key) {
    console.log(key);
});


Output:

a
b
c

Example 2: In this example, we are printing the valueof an object by the use of the lodash _.forIn() method.

javascript




// Defining Lodash variable
const _ = require('lodash');
 
let users = {
    'a': 1,
    'b': 2,
    'c': 3
};
 
_.forIn(users, function (value, key) {
    if (value > 1) {
        console.log(key, value);
    }
});


Output:

b 2
c 3

Note: This will not work in normal JavaScript because it requires the lodash library to be installed and can be installed using the following command:

npm install lodash


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads