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β
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β
| Object | Path | Result |
|---|---|---|
{a: {b: {c: 3}}} | 'a.b.c' | {a: {b: {}}} |
{x: 1, y: 2} | 'x' | {y: 2} |
Deprecated: Use destructuring or
omitfor 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" }
Deprecated: Use destructuring or