Lodash _.forIn() method
What you’ll learn
- How
_.forIn(object, iteratee)visits own and inherited enumerable string-keyed properties. - The iteratee signature:
(value, key, object). - How to stop iteration early by returning
false. - When to choose
_.forIn,_.forOwn, or nativefor...in.
Prerequisites
Know the difference between an object’s own properties and properties inherited through its prototype chain.
- Enumerable properties: only keys visible to property iteration are visited; non-enumerable properties are ignored.
- Prototype chain: inherited enumerable keys are included, which is the main difference from
_.forOwn.
Overview
_.forIn invokes an iteratee for every own and inherited enumerable string key on an object. It returns the original object, so it is best for side effects such as logging, validation, collecting keys, or controlled mutation.
Own + inherited
Use it when prototype-provided enumerable properties are intentionally part of the surface.
Early exit
Return false from the iteratee to stop looping before every key is visited.
Not a mapper
The return value is the original object. Build a result yourself if you need transformed output.
Syntax
_.forIn(object, [iteratee=_.identity]) - object: object to iterate over.
- iteratee: function invoked with
(value, key, object). - Early exit: return
falseto stop iteration. - Returns: the original object.
Iterate over key-value pairs
For a plain object, _.forIn feels similar to looping over Object.keys(), but the iteratee gives you value, key, and object directly.
import forIn from "lodash/forIn";
const user = {
name: "John",
age: 30,
city: "New York"
};
const lines = [];
forIn(user, (value, key) => {
lines.push(`${key}: ${value}`);
});
// lines -> ["name: John", "age: 30", "city: New York"] Inherited enumerable keys are included
This is the defining behavior. _.forIn visits enumerable properties from the prototype chain; _.forOwn does not.
import forIn from "lodash/forIn";
import forOwn from "lodash/forOwn";
function Profile(name) {
this.name = name;
}
Profile.prototype.role = "member";
const profile = new Profile("Ava");
const forInKeys = [];
forIn(profile, (value, key) => forInKeys.push(key));
const forOwnKeys = [];
forOwn(profile, (value, key) => forOwnKeys.push(key));
// forInKeys -> ["name", "role"]
// forOwnKeys -> ["name"] Stop early by returning false
Unlike forEach-style loops where developers often forget the break pattern, Lodash iteration helpers stop when the iteratee explicitly returns false.
import forIn from "lodash/forIn";
const config = {
host: "localhost",
port: 3000,
token: "",
retries: 3
};
let firstEmptyKey;
forIn(config, (value, key) => {
if (value === "") {
firstEmptyKey = key;
return false; // stop here
}
});
// firstEmptyKey -> "token" 📋 _.forIn vs _.forOwn vs native for...in
| Topic | _.forIn | _.forOwn | for...in |
|---|---|---|---|
| Own keys | Yes | Yes | Yes |
| Inherited enumerable keys | Yes | No | Yes |
| Symbol keys | No | No | No |
| Callback args | (value, key, object) | (value, key, object) | Key only |
| Early exit | Return false | Return false | break |
Most app code wants _.forOwn. Reach for _.forIn when inherited enumerable keys are intentionally meaningful.
Pitfalls to avoid
Prototype keys may surprise you
_.forIn includes inherited enumerable keys. Use _.forOwn when you only want fields stored directly on the object.
It returns the original object
Returning a transformed value from the iteratee does not build an output object. Use _.mapValues or _.transform for transformations.
Symbol keys are skipped
If your object uses symbols, use native reflection helpers such as Object.getOwnPropertySymbols() alongside regular key iteration.
Do not encode business rules in object order
Use arrays when order is part of the data model. Object key order can be predictable in many cases, but it is the wrong abstraction for ordered workflows.
❓ FAQ
Summary
- Purpose: iterate over own and inherited enumerable string keys.
- Remember: iteratee receives
(value, key, object); returnfalseto break; result is the original object. - Next: Lodash _.forInRight(), _.forOwn(), or the official Lodash docs for _.forIn.
_.forIn is closer to JavaScript's native for...in loop than to Object.keys(): it visits both own and inherited enumerable string keys. Use _.forOwn when inherited keys should be ignored.
6 people found this page helpful
