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 Object Properties:
4.2.
JavaScript
4.3.
Modifying Properties During Iteration:
4.4.
JavaScript
4.5.
Collecting Keys and Values:
4.6.
JavaScript
4.7.
Breaking Out of the Iteration:
5.
Frequently Asked Questions
5.1.
How does _.forOwn() differ from _.forIn()?
5.2.
Can _.forOwn() 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 _.forOwn() Method

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Iterating over an object's own properties (excluding inherited properties) is a frequent requirement in JavaScript programming. Lodash's _.forOwn() method is designed specifically for this task. It iterates over an object's own enumerable string keyed properties, executing a provided function for each property. 

Lodash _.forOwn() Method

This method is particularly useful in scenarios where you need to process or manipulate an object's properties while ignoring those from the prototype chain.

Why This Function is Used

The _.forOwn() function is used for iterating over an object's own properties, ensuring that only the properties directly belonging to the object (and not inherited) are considered. This is crucial in situations where inherited properties should not influence the operation, such as when manipulating data structures or applying transformations specific to an object's own properties.

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

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

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

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 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