Lodash _.forOwn() method
What you’ll learn
- How
_.forOwn(object, iteratee)visits an object’s own enumerable string keys only. - The iteratee signature
(value, key, object)and the early-exit pattern (return false). - Why
_.forOwnis the modern replacement for the oldfor...in+hasOwnPropertydance. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Familiarity with own vs inherited properties (or skim _.forIn() for the difference).
- Own properties: only keys assigned directly to the object are visited—same set as
Object.keys(). - Iteratee callbacks: remember the
(value, key, object)argument order—same as_.forIn. - Side effect mindset: the return value is the original object. Build a fresh result yourself if needed.
Overview
_.forOwn invokes the iteratee for every own enumerable string key on the object, ignoring anything on the prototype chain. It returns the original object, which makes it ideal for logging, validation, or in-place mutation of plain data objects.
Own keys only
Inherited keys are skipped automatically—no hasOwnProperty boilerplate.
Early exit
Return false from the iteratee to stop before every key is visited.
Not a mapper
Returns the original object. Use _.mapValues / _.transform if you need transformed output.
Syntax
_.forOwn(object, [iteratee=_.identity]) - object: object whose own enumerable string keys are iterated.
- iteratee: function invoked with
(value, key, object). - Early exit: return
falseto stop iteration. - Returns: the original object.
Iterate over an object’s own properties
For a plain object, _.forOwn visits each key once and gives you the value and key directly—cleaner than Object.keys(obj).forEach(...).
import forOwn from "lodash/forOwn";
const user = {
name: "John",
age: 30,
city: "New York"
};
const lines = [];
forOwn(user, (value, key) => {
lines.push(`${key}: ${value}`);
});
// lines -> ["name: John", "age: 30", "city: New York"] Inherited keys are skipped
This is the defining behavior. _.forOwn ignores enumerable keys on the prototype chain; _.forIn visits them.
import forOwn from "lodash/forOwn";
import forIn from "lodash/forIn";
function Profile(name) {
this.name = name;
}
Profile.prototype.role = "member";
const profile = new Profile("Ava");
const forOwnKeys = [];
forOwn(profile, (value, key) => forOwnKeys.push(key));
const forInKeys = [];
forIn(profile, (value, key) => forInKeys.push(key));
// forOwnKeys -> ["name"]
// forInKeys -> ["name", "role"] Mutate in place: drop empty values
Because the third iteratee argument is the original object, you can use _.forOwn to mutate or clean it in place.
import forOwn from "lodash/forOwn";
const personInfo = {
name: "Sam",
age: 22,
email: "sam@example.com",
address: ""
};
forOwn(personInfo, (value, key, obj) => {
if (value === "") {
delete obj[key];
}
});
// personInfo -> { name: "Sam", age: 22, email: "sam@example.com" } 📋 _.forOwn vs _.forIn vs Object.entries
| Topic | _.forOwn | _.forIn | Object.entries(...).forEach |
|---|---|---|---|
| Own keys | Yes | Yes | Yes |
| Inherited enumerable keys | No | Yes | No |
| Symbol keys | No | No | No |
| Callback args | (value, key, object) | (value, key, object) | ([key, value], index, entries) |
| Early exit | Return false | Return false | Throw or use for...of + break |
| Allocates | No | No | Yes (intermediate array) |
Most app code wants _.forOwn: it’s the safe default for plain data objects, with zero intermediate allocation.
Pitfalls to avoid
Picked the wrong helper
If you actually need prototype-provided enumerable keys (mixins, framework-defined behavior), use _.forIn. _.forOwn will silently skip them.
Returning a new value does nothing
The iteratee’s return value is only checked for false (early exit). Use _.mapValues or build a result object yourself for transformations.
Symbol keys are skipped
Use Object.getOwnPropertySymbols() alongside _.forOwn if your object has symbol keys.
Adding keys mid-iteration
Deleting the current key during iteration is safe. Adding new keys is implementation-defined—collect them and apply after the loop.
❓ FAQ
Summary
- Purpose: iterate over an object’s own enumerable string keys only.
- Remember: iteratee receives
(value, key, object); returnfalseto break; result is the original object. - Next: Lodash _.forOwnRight(), _.forIn(), or the official Lodash docs for _.forOwn.
_.forOwn is the modern stand-in for for (key in obj) + hasOwnProperty. It skips inherited prototype keys for you and gives the iteratee value, key, and the original object as separate arguments.
6 people found this page helpful
