Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax:
3.2.
Parameters:
3.3.
Return Value:
4.
Examples 
4.1.
Basic Iteration Over Properties:
4.2.
Using _.forIn() for Property Manipulation:
4.3.
JavaScript
4.4.
Breaking Out of the Iteration:
4.5.
Collecting Keys and Values:
4.6.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.forIn() differ from _.forEach()?
5.2.
Can _.forIn() be used with arrays?
5.3.
Is it safe to modify the object being iterated over?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.forIn() Method

Author Riya Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Iterating over an object's properties, including inherited ones, is a task frequently encountered in JavaScript programming. Lodash's _.forIn() method addresses this need by providing a way to iterate over all enumerable properties of an object, including those inherited through the prototype chain. 

Lodash _.forIn() Method

This method is particularly useful in scenarios where you need to examine or manipulate all properties of an object, regardless of whether they are own properties or inherited.

Why This Function is Used

The _.forIn() function is used to iterate over an object's own and inherited enumerable properties. This approach is essential in situations where you need to process or perform actions on all properties of an object, not just the properties the object directly possesses. It's especially helpful in dealing with objects that have a complex prototype chain or are part of an object-oriented model.

Syntax, Parameter and Return Value

Syntax:

_.forIn(object, [iteratee=_.identity])
You can also try this code with Online Javascript Compiler
Run Code

Parameters:

  • object (Object): The object to iterate over.
     
  • [iteratee=_.identity] (Function): The function invoked per iteration.

Return Value:

 Returns the passed-in object.

Examples 

Basic Iteration Over Properties:

var _ = require('lodash');
function Shape() {
  this.x = 0;
  this.y = 0;
}
Shape.prototype.move = function(x, y) {
  this.x += x;
  this.y += y;
};
var circle = new Shape();
circle.radius = 5;
_.forIn(circle, function(value, key) {
  console.log(key, value);
});
You can also try this code with Online Javascript Compiler
Run Code


Output might include 'x', 'y', 'radius', 'move'

Demonstrates iterating over both own and inherited properties of an object.

Using _.forIn() for Property Manipulation:

  • JavaScript

JavaScript

var data = { 'a': 1, 'b': 2 };

Object.prototype.c = 3;

_.forIn(data, function(value, key) {

 data[key] = value * value;

});

console.log(data);
You can also try this code with Online Javascript Compiler
Run Code

Output: 

{ 'a': 1, 'b': 4, 'c': 9 }


Shows how to manipulate each property in an object, including inherited ones.

Breaking Out of the Iteration:

var object = { 'a': 1, 'b': 2, 'c': 3 };
_.forIn(object, function(value, key) {
  if (key === 'b') {
    return false; // Break out of the iteration
  }
});
You can also try this code with Online Javascript Compiler
Run Code


An example of breaking the iteration based on a condition.

Collecting Keys and Values:

  • JavaScript

JavaScript

var object = { 'a': 1, 'b': 2 };

var keys = [];

var values = [];

_.forIn(object, function(value, key) {

 keys.push(key);

 values.push(value);

});

console.log(keys);

console.log(values);
You can also try this code with Online Javascript Compiler
Run Code

Output:

['a', 'b']
[1, 2]


Demonstrates collecting keys and values from an object.

Frequently Asked Questions

How does _.forIn() differ from _.forEach()?

_.forIn() iterates over all enumerable properties, including inherited ones, whereas _.forEach() only iterates over an object's own enumerable properties.

Can _.forIn() be used with arrays?

While _.forIn() can technically be used with arrays, it is not the most suitable method for array iteration due to its inclusion of inherited properties. Array-specific methods like _.forEach() are generally more appropriate for arrays.

Is it safe to modify the object being iterated over?

Modifying the object during iteration can lead to unexpected behavior, so it's generally advisable to avoid directly modifying the object's structure (like adding or removing properties) during iteration.

Conclusion

Lodash's _.forIn() method provides a straightforward way to iterate over all enumerable properties of an object, including those inherited through the prototype chain. It's particularly useful for comprehensive property processing in complex objects.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass