Skip to content

unset

Removes the value at a dot/bracket-notation path or explicit key array, mutating the object in place. Uses the same path syntax as get/set.

A missing intermediate segment is a no-op (nothing to remove), not an error. As with set, any path containing a string segment equal to __proto__, constructor, or prototype is rejected and the object is returned unchanged.

The removed key stops appearing in Object.keys/for...in — unlike setting it to undefined, which would keep the key present.

Available since v3.0.0

import { unset } from '@helpers4/object';
// or, from the all-in-one package (same code, one install):
import { unset } from 'helpers4/object';
unset<T extends object>(obj: T, path: string | readonly PropertyKey[]): T
ParameterTypeDescription
objTThe object to mutate
pathstring | readonly PropertyKey[]Dot/bracket-notation string, or explicit `PropertyKey[]`

T — The same object reference, with the key removed if it existed

Uses the same dot/bracket path syntax as get() and set().

const config = { server: { host: 'localhost', debug: true } };
unset(config, 'server.debug')
// => { server: { host: 'localhost' } }

Unlike set(), unset() never creates intermediate objects — there is nothing to remove.

unset({ a: 1 }, 'x.y.z')
// => { a: 1 }  (unchanged)

View source on GitHub