Skip to content

omitBy

Creates a new object without the own enumerable entries for which predicate returns true.

Complements omit for when the keys to remove aren’t known ahead of time — omit takes an explicit key list, omitBy takes a predicate.

Available since v3.0.0

import { omitBy } from '@helpers4/object';
// or, from the all-in-one package (same code, one install):
import { omitBy } from 'helpers4/object';
omitBy<T extends Record<string, unknown>>(obj: T | null | undefined, predicate: function): Partial<T> | null | undefined
ParameterTypeDescription
objT | null | undefinedThe source object. `null`/`undefined` pass through unchanged.
predicatefunctionCalled with `(value, key)` for each own enumerable entry

Partial<T> | null | undefined — A new object without the matching entries

Unlike omit(), the keys to drop don’t need to be known ahead of time.

omitBy({ a: 1, b: undefined, c: 2 }, (value) => value === undefined)
// => { a: 1, c: 2 }

The predicate also receives the key, so filtering by name works too.

omitBy({ id: 1, name: 'x', _internal: true }, (_v, key) => key.startsWith('_'))
// => { id: 1, name: 'x' }

View source on GitHub