Lodash _.forOwn() method Iterates over the own keys of the given object and invokes iteratee for each property. The iteratee function is invoked with three arguments: (value, key, object). Iteratee function may exit iteration early by explicitly returning false.
Syntax:
_.forOwn( 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 and value of the given object by the use of the lodash _forOwn() method.
javascript
const _ = require('lodash');
let users = {
'a': 1,
'b': 2,
'c': 3
};
_.forOwn(users, function (value, key) {
console.log(key, '=', value);
});
|
Output:
a = 1
b = 2
c = 3
Example 2: In this example, we are printing the key and having greater than 2 value of the given object by the use of the lodash _forOwn() method.
javascript
const _ = require('lodash');
let users = {
'a': 1,
'b': 2,
'c': 3
};
_.forOwn(users, function (value, key) {
if (value > 2) {
console.log(key, value);
}
});
|
Output:
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
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
30 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...