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
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
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 DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.