setPath
function setPath<T extends object>(
target: T,
path: string | Array<string | number>,
value: unknown
): T;
Missing links are created along the way: a numeric segment makes an array, a string segment an object. setPath({}, ["rows", 0, "id"], 7) therefore produces { rows: [{ id: 7 }] }.
The target is modified in place and returned, so the call composes but does not copy.
Parameters
Parameter | Type | Description |
|---|---|---|
|
| The object to write into. It is modified. |
|
| A dotted string, or an array of keys and indices. |
|
| The value to write. |
Returns
T — the same object, now carrying the value.
Throws
Exception | Condition |
|---|---|
| the target is not an object, or the path is empty. |
Examples
Writing
setPath({}, "a.b", 1); // => { "a": { "b": 1 } }
setPath({}, ["rows", 0, "id"], 7); // => { "rows": [{ "id": 7 }] }
See also
Source
23 September 2026
This documentation was generated with AI (Claude) from the library's source code.