Object
unset

🎯 Removes a property at a specific path of an object.

⚠️

Mutable: This function mutates the original object. Use toUnset for an immutable version.

When used on arrays, it creates a sparse array (opens in a new tab) by deleting the element at the index without shifting other elements.

Syntax

import { unset } from '@opentf/std';
 
unset<T>(
  obj: T,
  path: string | unknown[],
): T

Parameters

  • obj: The object or array to modify.
  • path: The path of the property to remove.

Returns

The modified object.

Examples

unset({ a: 1, b: 2 }, 'a') //=> { b: 2 }
 
unset({ a: 1, b: 2 }, 'c') //=> { a: 1, b: 2 } (Non-existent path)
 
// Sparse array behavior
unset([1, 2, 3], '1') //=> [1, <1 empty item>, 3]
 
const nestedObj = { x: { y: { z: ['a', null, 'b'] } } };
unset(nestedObj, 'x.y.z') //=> { x: { y: {} } }

Related

Try