GuobaGuoba Utils

set

@guoba-ai/utils / object / set

Function: set()

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

Defined in: object.ts:405

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.

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 }] }

const obj = { x: 1 }
set(obj, 'y', 2) === obj // true

set({}, '__proto__.polluted', 1) // throws TypeError

Warning

This function mutates obj, only supports dot-separated paths, and rejects unsafe segments such as __proto__.

On this page