Lodash _.pickBy() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 6 Examples + 3 Try It
Object utilities

What You’ll Learn

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.

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.

📝 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.
javascript
import pickBy from "lodash/pickBy";

const user = {
  name: "Alice",
  age: 30,
  isAdmin: true,
  isActive: false
};

const adminFlags = pickBy(user, (value) => value === true);
// -> { isAdmin: true }

⚡ Quick Reference

TaskCode patternResult
Keep strict true_.pickBy(obj, v => v === true)Boolean flags only
Keep non-null_.pickBy(obj, v => v != null)Drops null & undefined
Keep numbers > 0_.pickBy(obj, v => typeof v === "number" && v > 0)Positive counts
Keep by key prefix_.pickBy(obj, (v, k) => k.startsWith("enable"))Naming rule
Fixed key list_.pick(obj, ["a", "b"])Use _.pick()
Inverse helper_.omitBy(obj, predicate)Drop where predicate is truthy
Rule
truthy → keep

Core behavior

Mutates?
No

Returns new object

Args
(value, key)

Predicate signature

Pair with
_.omitBy()

Inverse selection

🧰 Parameters

Arguments to _.pickBy() and how the predicate is evaluated:

object Required

The source object. Lodash iterates own enumerable string keys. null and undefined return {}.

_.pickBy(record, predicate)
predicate Optional

Function invoked as predicate(value, key). Return truthy to keep the property. When omitted, truthy values are kept.

(value, key) => value != null
value 1st arg

The property value. Use for type checks, comparisons, and emptiness tests.

_.pickBy(obj, v => v > 0)
key 2nd arg

The property name (string). Use for prefix/suffix rules and internal vs public field names.

_.pickBy(obj, (v, k) => !k.startsWith("_"))

Built-in helpers like _.isNumber, _.isString, and _.negate(_.isNil) can be passed directly as predicates when they match your intent.

Examples Gallery

Real-world _.pickBy() patterns with sample output and interactive Try It Yourself labs.

📚 Getting Started

Keep properties that match a simple value condition.

Example 1 — Pick enabled feature flags

Keep only properties whose value is strictly true—useful for toggles and boolean config maps.

javascript
const featureFlags = {
  enableExperimentalFeature: false,
  showBetaBanner: true,
  displayNewUI: false
};

const enabled = _.pickBy(featureFlags, (value) => value === true);

console.log(enabled);
// -> { showBetaBanner: true }
Try It Yourself

How It Works

value === true is stricter than a truthy check—it excludes non-boolean truthy values like 1 or "yes".

Example 2 — Keep non-null values

Filter out null and undefined while preserving 0, false, and empty strings when they matter.

javascript
const sportsEquipment = {
  ball: "football",
  racket: "tennis",
  skates: null,
  helmet: undefined
};

const valid = _.pickBy(sportsEquipment, (value) => value != null);

console.log(valid);
// -> { ball: "football", racket: "tennis" }
Try It Yourself

How It Works

value != null catches both null and undefined without treating 0 or false as missing.

📈 Practical Patterns

Inventory filters, user preferences, and key-based selection rules.

Example 3 — In-stock inventory items

Keep products whose nested quantity is greater than zero.

javascript
const inventory = {
  apple: { quantity: 5, price: 2 },
  banana: { quantity: 10, price: 1 },
  orange: { quantity: 0, price: 3 }
};

const inStock = _.pickBy(inventory, (item) => item.quantity > 0);

console.log(inStock);
// -> {
//      apple: { quantity: 5, price: 2 },
//      banana: { quantity: 10, price: 1 }
//    }
Try It Yourself

How It Works

The predicate inspects each top-level value. Nested objects are kept whole when the condition passes—_.pickBy() does not drill into them.

Example 4 — Active user preferences

Extract boolean preferences that are turned on for a settings summary panel.

javascript
const userPreferences = {
  darkMode: true,
  notifications: false,
  fontSize: "medium",
  compactLayout: true
};

const activePrefs = _.pickBy(userPreferences, (value) => value === true);

console.log(activePrefs);
// -> { darkMode: true, compactLayout: true }

Example 5 — Pick by key naming rule

Keep only public fields by excluding keys that start with an underscore.

javascript
const model = {
  id: 1,
  title: "Hello",
  _internalId: "abc-123",
  _cacheKey: "xyz"
};

const publicFields = _.pickBy(model, (value, key) => !key.startsWith("_"));

console.log(publicFields);
// -> { id: 1, title: "Hello" }

🚀 Beyond the Basics

Mirror helpers and when fixed _.pick() is clearer.

Example 6 — _.pickBy vs _.omitBy

Same predicate, opposite outcome—choose whichever reads clearer in your codebase.

javascript
const stats = { views: 10, likes: 0, shares: 5, drafts: 0 };

const nonZero = _.pickBy(stats, (value) => value > 0);
// -> { views: 10, shares: 5 }

const withoutZeros = _.omitBy(stats, (value) => value === 0);
// -> { views: 10, shares: 5 }

When to prefer _.pick()

If the key list never changes, _.pick() with a constant array is simpler than a predicate that checks the same names every time.

🧠 How _.pickBy() Works

1

Iterate own keys

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.

📝 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.

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().

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about _.pickBy()

Use these when filtering objects with dynamic keep-rules.

5
Core concepts
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.

Practice _.pickBy() in the Live Editor

Run the examples and experiment with your own predicate functions.

Open Try It editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful