Skip to main content

unset

Removes the property at the given path of an object and returns the resulting object.

It uses the same dot notation paths as get and set. If the type of the given object has optional properties, they must be accessed with an optional (?) path.

If a value in the middle of the path is missing or is not an object, nothing is removed and the object is returned as-is.

By default the original object is not mutated (immutable: true). Passing immutable: false removes the property directly from the original object and returns that same object.


Code​

🔗 View source code


Interface​

typescript
function unset<T extends Record<string, unknown>, P extends PropertyPath<T>>(
obj: T,
path: P,
options?: { immutable?: boolean }
): T;

Parameters​

NameTypeDefaultDescription
objT extends Record<string, unknown>-The object to remove the property from.
pathP extends PropertyPath<T>-The path of the property to remove.
options.immutablebooleantrueIf false, removes the property directly from the original object without copying.

Usage​

Basic Usage​

typescript
import { unset } from '@modern-kit/utils';

const obj = { a: { b: 1, c: 2 } };

unset(obj, 'a.b');
// { a: { c: 2 } }
// obj: { a: { b: 1, c: 2 } } (original is preserved)

Optional path​

typescript
import { unset } from '@modern-kit/utils';

// Returns as-is when a value in the middle of the path is missing.
const obj: { a?: { b?: number } } = {};

unset(obj, 'a?.b');
// {}

Mutating the original object​

typescript
import { unset } from '@modern-kit/utils';

const obj = { a: { b: 1, c: 2 } };

unset(obj, 'a.b', { immutable: false });
// obj: { a: { c: 2 } }