By the end of this tutorial, you’ll use _.omitBy() to remove object properties based on dynamic rules—not just fixed key names.
01
Predicate basics
Write (value, key) => boolean functions that decide what to drop.
02
Truthy = omit
When the predicate returns truthy, that property is excluded.
03
Key patterns
Omit by prefix, suffix, or naming convention (is*, _*).
04
Value filtering
Strip null, empty strings, or other unwanted values safely.
05
omit vs omitBy
Choose fixed deny-lists vs runtime predicate rules.
06
Production tips
Avoid the !value trap, keep predicates fast, test edge cases.
Fundamentals
What Is _.omitBy()?
_.omitBy() is Lodash’s predicate-driven omit helper. Instead of listing exact key names like _.omit(), you pass a function. Lodash walks each own enumerable string key on the object; if your predicate returns a truthy value for that property, it is left out of the result.
💡
Remember the rule
Truthy predicate → omit. Falsy predicate → keep. This is the opposite mental model from filtering arrays with .filter(), where truthy means keep.
Reach for _.omitBy() when removal rules depend on values, key naming patterns, or runtime flags—for example stripping every key that starts with password or dropping null fields from a form payload.
Foundation
📝 Syntax
The signature pairs an object with one predicate function:
javascript
_.omitBy(object, predicate)
Syntax Rules
object — source object to process (not mutated).
predicate(value, key) — called for each own enumerable string key.
Truthy return — property is omitted from the result.
Falsy return — property is copied into the new object.
Shallow — top-level keys only; nested objects are not traversed.
_.isNil is null or undefined only. A naive value => !value predicate would strip legitimate falsy data like zero counts and empty-but-intentional flags.
Example 4 — Sanitize API responses
Strip any field whose key starts with password before returning JSON from an Express route.
Lodash walks each own enumerable string key on the source object.
Input
2
Call predicate
predicate(value, key) runs for each property.
Test
3
Branch on result
Truthy → skip key. Falsy → shallow-copy value into the result object.
Filter
=
📦
Filtered copy returned
Original object unchanged. Result contains only properties that failed the omit test.
Important
📝 Notes
Truthy omits—the predicate returning true means exclude that key.
_.omitBy() is non-mutating and works on top-level keys only.
value => !value removes 0, false, and ""—often not what you want.
Prefer _.isNil when you only mean null and undefined.
Keep predicates fast on large objects; avoid heavy work inside the callback.
For a fixed key list, _.omit() is simpler and easier to read.
Wrap Up
Conclusion
_.omitBy() shines when property removal depends on a rule rather than a fixed list. Write a clear predicate, remember that truthy means omit, and test edge cases like 0 and false before shipping to production.
When keys are known ahead of time, use _.omit(). When you want to keep matching properties instead, reach for _.pickBy() or the upcoming _.pick() tutorial.
Write small, named predicate functions for readability
Use key for naming rules and value for content rules
Prefer _.isNil over !value when cleaning optional fields
Test with 0, false, and empty string edge cases
Reuse predicates across API sanitizers and form helpers
❌ Don’t
Assume !value only removes null/undefined
Put slow I/O or network calls inside the predicate
Use _.omitBy() when _.omit() with a key array is enough
Forget that nested objects are not recursively processed
Rely on predicates alone for security without reviewing schema changes
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about _.omitBy()
Use these when filtering objects with dynamic rules.
5
Core concepts
⚙️01
Predicate
(value, key) => …
Basics
❌02
Truthy
Means omit key.
Rule
🔑03
Patterns
Prefix/suffix rules.
Keys
⚠️04
!value trap
Use _.isNil.
Pitfall
🔀05
pickBy
Inverse helper.
Related
❓ Frequently Asked Questions
_.omitBy() creates a new object by copying own enumerable string-keyed properties except those where the predicate function returns a truthy value. A truthy result means omit that property.
The predicate is called as predicate(value, key). Use value for content-based rules (is null, is empty string) and key for name-based rules (starts with underscore, ends with Id).
No. Like _.omit(), it returns a new object and leaves the source unchanged.
_.omit() takes a fixed list of key names. _.omitBy() uses a function so removal can depend on runtime conditions, patterns, or values.
They are opposites. _.omitBy() drops properties where the predicate is truthy. _.pickBy() keeps properties where the predicate is truthy.
Yes. Because 0, false, and empty string are falsy in JavaScript, value => !value omits them too. Use _.isNil for only null/undefined, or write an explicit check when 0 and false must be kept.
Did you know?
_.omitBy and _.pickBy are mirror images: the same predicate keeps properties in pickBy but drops them in omitBy. Lodash also provides ready-made predicates like _.isNil and _.isEmpty you can pass directly.