Syntax, Parameter and Return Value
Syntax:
_.forOwn(object, [iteratee=_.identity])
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 Object Properties:
JavaScript
var _ = require('lodash');
var user = { 'name': 'John', 'age': 30, 'active': true };
_.forOwn(user, function(value, key) {
console.log(key, value);
});

You can also try this code with Online Javascript Compiler
Run Code
Output:
'name' John, 'age' 30, 'active' true
Demonstrates iterating over an object's own properties.
Modifying Properties During Iteration:
JavaScript
var data = { 'a': 1, 'b': 2, 'c': 3 };
_.forOwn(data, function(value, key) {
data[key] = value * 2;
});
console.log(data);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ 'a': 2, 'b': 4, 'c': 6 }
Shows how to modify each property's value during iteration.
Collecting Keys and Values:
JavaScript
var keys = [];
var values = []
_.forOwn(user, 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:
['name', 'age', 'active']
['John', 30, true]

You can also try this code with Online Javascript Compiler
Run Code
An example of collecting an object's keys and values separately.
Breaking Out of the Iteration:
var object = { 'a': 1, 'b': 2, 'c': 3 };
_.forOwn(object, function(value, key) {
if (value === 2) {
return false; // Break out of the iteration
}
});

You can also try this code with Online Javascript Compiler
Run Code
Demonstrates breaking the iteration based on a condition.
Frequently Asked Questions
How does _.forOwn() differ from _.forIn()?
_.forOwn() iterates only over an object's own properties, while _.forIn() iterates over both the object's own properties and inherited properties.
Can _.forOwn() be used with arrays?
While _.forOwn() can be used with arrays, it is primarily designed for objects. For arrays, methods like _.forEach() are more appropriate.
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 _.forOwn() method provides an effective way to iterate over an object's own properties, excluding any inherited properties. It's particularly useful for tasks that require processing or manipulating an object's direct properties without interference from the prototype chain.
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.