🎯 Sets the value at a specific path of an object.
⚠️
Mutable: This function mutates the original object. Use toSet for an immutable version.
Syntax
import { set } from '@opentf/std';
set<T>(
obj: T,
path: string | unknown[],
value: unknown | ((val: unknown) => unknown)
): TParameters
obj: The object to modify.path: The path of the property to set.value: The value to set, or a callback function that receives the current value and returns the new value.
Returns
The modified object.
Examples
set({}, 'a', 1) //=> { a: 1 }
set({}, 'a.b', 25) //=> { a: { b: 25 } }
set({}, 'user.email', 'user@example.com')
//=> { user: { email: 'user@example.com' } }
set({}, 'fruits[0]', 'Apple') //=> { fruits: ['Apple'] }
// Using a callback to update based on current value
set({ a: 1 }, 'a', (val) => val + 1) //=> { a: 2 }
// Automatically creates arrays if indices are used in path
set({}, 'items[2].id', 1) //=> { items: [undefined, undefined, { id: 1 }] }