By the end of this tutorial, you’ll use _.pickBy() to keep object properties that match dynamic rules—not just fixed key names.
01
Predicate basics
Write (value, key) => boolean functions that decide what to keep.
02
Truthy = keep
When the predicate returns truthy, that property stays in the result.
03
Value filters
Keep non-null values, numbers > 0, or strict true flags.
04
Key patterns
Select properties by prefix, suffix, or naming convention.
05
pickBy vs pick
Choose predicate rules vs fixed allow-lists.
06
Production tips
Pair with _.omitBy(), keep predicates fast, test edge cases.
Fundamentals
What Is _.pickBy()?
_.pickBy() is Lodash’s predicate-driven pick helper. You pass an object and a function; Lodash copies each own enumerable string key whose predicate(value, key) returns a truthy value. Everything else is left out. It is the mirror image of _.omitBy(), which drops properties when the predicate is truthy.
💡
Remember the rule
Truthy predicate → keep. Falsy predicate → exclude. This is the opposite of _.omitBy(), where truthy means omit.
Use _.pickBy() for feature flags, inventory filters, cleaning optional form fields, and any case where what you keep depends on values or key names rather than a static list.
Foundation
📝 Syntax
The signature pairs an object with an optional predicate function:
javascript
_.pickBy(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 included in the result.
Falsy return — property is excluded.
Default predicate — if omitted, Lodash keeps properties with truthy values.
Shallow — top-level keys only; nested objects are not traversed.
Lodash walks each own enumerable string key on the source object.
Input
2
Run predicate
predicate(value, key) evaluates each property.
Test
3
Copy passing keys
Truthy results are shallow-copied into a fresh result object.
Copy
=
📌
Filtered object returned
Only matching properties remain. The source object is unchanged.
Important
📝 Notes
Truthy keeps—the predicate returning true means include that key.
_.pickBy() is non-mutating and works on top-level keys only.
value === true is not the same as a truthy check—be explicit about boolean flags.
Calling _.pickBy(obj) without a predicate keeps properties with truthy values.
Nested objects inside kept keys are shared by reference (shallow copy).
For a fixed key list, _.pick() is usually clearer.
Wrap Up
Conclusion
_.pickBy() is Lodash’s flexible pick helper: write a predicate, keep the properties that pass, and return a new object without touching the original. It fits feature flags, inventory filters, null-safe cleanup, and any dynamic allow-rule.
When keys are fixed, use _.pick(). When you want to drop matches instead of keep them, use _.omitBy().
Name predicates clearly (isEnabledFlag, isPublicKey)
Use strict checks (=== true, != null) when intent matters
Prefer _.pick() when the key set is static
Test edge cases: 0, false, empty string, nested objects
Keep predicate logic fast on large objects
❌ Don’t
Confuse truthy checks with strict boolean flag checks
Assume nested properties are filtered recursively
Put heavy computation or async work inside the predicate
Use _.pickBy() when three fixed keys would read simpler with _.pick()
Forget that _.omitBy is the inverse, not identical
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about _.pickBy()
Use these when filtering objects with dynamic keep-rules.
5
Core concepts
⚙️01
Predicate
(value, key) => …
Basics
✅02
Truthy
Means keep key.
Rule
🎯03
Flags
v === true.
Pattern
📐04
Shallow
Top-level only.
Depth
🔀05
omitBy
Inverse helper.
Related
❓ Frequently Asked Questions
_.pickBy() creates a new object containing only the own enumerable string-keyed properties where the predicate function returns a truthy value. A truthy result means keep that property.
The predicate is called as predicate(value, key). Use value for content checks (is a number, is not null) and key for naming rules (starts with enable, ends with At).
No. Like _.pick() and _.omitBy(), it returns a new object and leaves the source unchanged.
_.pick() keeps a fixed list of key names. _.pickBy() keeps properties that pass a runtime condition, so the result can change as values change.
They are opposites. _.pickBy() keeps properties where the predicate is truthy. _.omitBy() drops properties where the predicate is truthy.
If you call _.pickBy(object) without a predicate, Lodash uses an identity-style check and keeps properties whose values are truthy. Prefer passing an explicit predicate for clarity.
Did you know?
_.pickBy and _.omitBy use the same predicate signature but opposite outcomes. You can often rewrite one in terms of the other with _.negate, though picking the helper that matches how you think about the problem (keep these vs drop these) usually produces clearer code.