Lodash _.omitBy() 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 _.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.

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.

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

const user = {
  id: 1,
  username: "john_doe",
  email: "john@example.com",
  isAdmin: true
};

const publicUser = omitBy(user, (value, key) => key === "isAdmin");
// -> { id: 1, username: "john_doe", email: "john@example.com" }

⚡ Quick Reference

TaskCode patternResult
Omit one key_.omitBy(obj, (v, k) => k === "secret")Drop matching key
Omit key prefix_.omitBy(obj, (v, k) => k.startsWith("is"))Drop isAdmin, isActive, etc.
Omit null/undefined_.omitBy(obj, _.isNil)Keep 0 and false
Omit empty strings_.omitBy(obj, v => v === "")Explicit empty check
Fixed key list_.omit(obj, ["a", "b"])Use _.omit() instead
Opposite helper_.pickBy(obj, predicate)Keep where predicate is truthy
Rule
truthy → omit

Core behavior

Mutates?
No

Returns new object

Args
(value, key)

Predicate signature

Pair with
_.pickBy()

Inverse selection

🧰 Parameters

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

object Required

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

_.omitBy(record, predicate)
predicate Required

Function invoked as predicate(value, key). Return a truthy value to omit that property; falsy to keep it.

(value, key) => key.startsWith("_")
value 1st arg

The property value. Use for content checks: _.isNil(value), value === "", typeof value === "string".

_.omitBy(obj, _.isNil)
key 2nd arg

The property name (string). Use for naming rules: prefixes, suffixes, private/internal markers.

_.omitBy(obj, (v, k) => k.endsWith("Id"))

Lodash also ships built-in predicates like _.isNil, _.isEmpty, and _.isUndefined that work well as omitBy callbacks.

Examples Gallery

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

📚 Getting Started

Omit properties when a simple key or value condition is true.

Example 1 — Omit by key name

Remove the isAdmin flag from a user object before sending it to the client.

javascript
const user = {
  id: 1,
  username: "john_doe",
  email: "john@example.com",
  isAdmin: true
};

const publicUser = _.omitBy(user, (value, key) => key === "isAdmin");

console.log(publicUser);
// -> { id: 1, username: "john_doe", email: "john@example.com" }
Try It Yourself

How It Works

For isAdmin, the predicate returns true, so that key is omitted. All other keys return falsy and are copied to the new object.

Example 2 — Omit keys by prefix

Drop every boolean flag whose name starts with is—handy for stripping internal state from a product record.

javascript
const product = {
  id: 1,
  name: "Laptop",
  price: 999,
  isAvailable: true,
  isFeatured: false
};

const catalogItem = _.omitBy(product, (value, key) => key.startsWith("is"));

console.log(catalogItem);
// -> { id: 1, name: "Laptop", price: 999 }
Try It Yourself

How It Works

Key-based predicates scale better than hard-coding every flag name. The same pattern works for _ prefixes on private fields.

📈 Practical Patterns

Sanitize data, clean form payloads, and filter API responses with value-based rules.

Example 3 — Omit null and undefined safely

Use Lodash’s _.isNil to drop only null and undefined—without accidentally removing 0 or false.

javascript
const edgeCase = {
  a: null,
  b: undefined,
  c: 0,
  d: "",
  e: false
};

const cleaned = _.omitBy(edgeCase, _.isNil);
// -> { c: 0, d: "", e: false }

// ⚠ Avoid value => !value — it also omits 0, "", and false
Try It Yourself

How It Works

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

javascript
app.get("/user/:id", (req, res) => {
  const userData = fetchUserFromDb(req.params.id);

  const safe = _.omitBy(userData, (value, key) =>
    key.startsWith("password") || key.startsWith("internal")
  );

  res.json(safe);
});

Example 5 — Clean form payload before submit

Remove empty optional fields so the server receives only meaningful values.

javascript
const formState = {
  title: "My post",
  subtitle: "",
  tags: ["lodash"],
  notes: null,
  published: false
};

const payload = _.omitBy(formState, (value) =>
  value === "" || value === null || value === undefined
);

// -> { title: "My post", tags: ["lodash"], published: false }

🚀 Beyond the Basics

Understand how _.omitBy() pairs with _.pickBy() and when fixed _.omit() is simpler.

Example 6 — _.omitBy vs _.pickBy

_.pickBy() keeps properties where the predicate is truthy. _.omitBy() drops them. Choose whichever reads clearer in your team’s code.

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 }

🧠 How _.omitBy() Works

1

Iterate own keys

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.

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

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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about _.omitBy()

Use these when filtering objects with dynamic rules.

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

Practice _.omitBy() 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