Skip to main content

Function: setNestedProperty()

function setNestedProperty(
obj,
field,
value): void;

Sets or deletes a nested property in an object using PHP-style bracket notation. If value is null or undefined, deletes the property and cleans up empty parents.

Parameters

ParameterTypeDescription
objanyThe object to modify.
fieldstringThe field path in PHP-style bracket notation (e.g. 'a[b][c]').
valueanyThe value to set. If null or undefined, the property is deleted.

Returns

void

Example

setNestedProperty(obj, 'a[b][c]', 123) // obj.a.b.c = 123
setNestedProperty(obj, 'a[b][c]', null) // deletes obj.a.b.c