Skip to main content

unset()

unset<T>(object, path): T

Removes the property at path of object, returning a new object (immutable).

DEPRECATED

Use destructuring or omit for immutable property removal.


Type Parametersโ€‹

T: T extends objectโ€‹

The type of the input object.


Parametersโ€‹

object: Tโ€‹

The object to modify.

path: string | (string | number | symbol)[]โ€‹

The path of the property to unset.


Returns: Tโ€‹

A new object with the property removed.


See Alsoโ€‹

omit


Sinceโ€‹

2.0.0


Also known asโ€‹

dissocPath (Ramda) ยท unset (Lodash, es-toolkit) ยท โŒ (Remeda, Radashi, Effect, Modern Dash, Antfu)


Exampleโ€‹

const object = { a: { b: { c: 3 } } };

// โŒ Deprecated approach
unset(object, 'a.b.c');
// => { a: { b: {} } }

// โœ… Recommended approach (for shallow properties)
const { c, ...rest } = object.a.b;
// rest = {}

// โœ… Recommended approach (for nested properties)
import { omit } from '@pithos/arkhe/object/omit';
// or use immutability-helper, immer, etc.

How it works?โ€‹

Removes property at path, returning new object (immutable).

Processing Flowโ€‹

Common Inputsโ€‹

ObjectPathResult
{a: {b: {c: 3}}}'a.b.c'{a: {b: {}}}
{x: 1, y: 2}'x'{y: 2}

warning Deprecated: Use destructuring or omit for immutable removal.


Use Casesโ€‹

Remove nested property ๐Ÿ“Œโ€‹

Remove property at path immutably.

// For shallow properties
const { propToRemove, ...rest } = obj;

// For nested, use omit or destructuring
import { omit } from '@pithos/arkhe';

Clean objectโ€‹

Remove unwanted properties.

const { password, ...safeUser } = user;

Remove nested state property immutablyโ€‹

Delete a deeply nested property in a state tree without mutating the original. Essential pattern for Redux/Zustand reducers handling deletion actions.

const state = {
users: {
"u-1": { name: "Alice", preferences: { theme: "dark", notifications: true } },
"u-2": { name: "Bob", preferences: { theme: "light" } },
},
};

// Remove a specific user's notification preference
const newState = unset(state, "users.u-1.preferences.notifications");
// state.users["u-1"].preferences.notifications still exists (original untouched)
// newState.users["u-1"].preferences => { theme: "dark" }