GuobaGuoba Utils

set

@guoba-ai/utils / object / set

Function: set()

function set<T>(
   obj, 
   path, 
   value): T;

Defined in: object.ts:321

Set a value at a deep path, creating intermediate objects or arrays as needed. Mutates and returns the original object. If a path segment is a numeric string, an array is created for that level.

Type Parameters

T

T extends object

Parameters

obj

T

The target object

path

string

Dot-separated path string (e.g. 'a.b.c')

value

unknown

The value to set

Returns

T

The original object (mutated)

Example

set({}, 'a.b.c', 42) // { a: { b: { c: 42 } } }
set({}, 'a.0.b', 1) // { a: [{ b: 1 }] }

On this page