Skip to content

Latest commit

 

History

History
69 lines (46 loc) · 2.42 KB

File metadata and controls

69 lines (46 loc) · 2.42 KB

omit / omitUndefined / omitNull / omitNullish

These functions provide convenient ways to exclude specific properties, undefined values, or null values from an object. They are built on top of the merge function.

Functions

omit(obj, keys)

Creates a new object that includes all properties of obj except the specified keys. This is a shallow operation.

import { omit } from '@jsopen/objects';

const original = { a: 1, b: 2, c: 3 };
const result = omit(original, ['b', 'c']);
// result is { a: 1 }

omitUndefined(obj, deep?)

Creates a new object by excluding all properties that have undefined as their value.

  • deep: false (default): Only top-level undefined values are removed.
  • deep: true: Recursively removes undefined values from plain objects and arrays.
  • deep: 'full': Recursively removes undefined values from all objects including class instances.
import { omitUndefined } from '@jsopen/objects';

const original = { a: 1, b: undefined, c: { d: undefined } };
const result = omitUndefined(original, true);
// result is { a: 1, c: {} }

omitNull(obj, deep?)

Creates a new object by excluding all properties that have null as their value.

  • deep: false (default): Only top-level null values are removed.
  • deep: true: Recursively removes null values from plain objects and arrays.
  • deep: 'full': Recursively removes null values from all objects including class instances.
import { omitNull } from '@jsopen/objects';

const original = { a: 1, b: null, c: { d: null } };
const result = omitNull(original, true);
// result is { a: 1, c: {} }

omitNullish(obj, deep?)

Creates a new object by excluding all properties that have either null or undefined as their value.

  • deep: false (default): Only top-level nullish values are removed.
  • deep: true: Recursively removes nullish values from plain objects and arrays.
  • deep: 'full': Recursively removes nullish values from all objects including class instances.
import { omitNullish } from '@jsopen/objects';

const original = { a: 1, b: null, c: undefined, d: { e: null } };
const result = omitNullish(original, true);
// result is { a: 1, d: {} }

Options

These functions use standard merge options internally. omitUndefined, omitNull, and omitNullish preserve property descriptors by default (copyDescriptors: true).