fn
set
v0.0.10testedWrite a deeply nested value into a collection by a dot-separated path, creating any missing intermediate containers along the way. A numeric next segment creates an array, otherwise an object. Mutates and returns the original collection.
Example
ts
set({ user: { name: 'John' } }, 'user.name', 'Jane'); // { user: { name: 'Jane' } }
set({}, 'items.0.id', 1); // { items: [{ id: 1 }] }Signature
ts
export function set<O extends Collection>(obj: O, path: string, value: unknown): O{ ... }Type Parameters
Oextends CollectionParameters
| Parameter | Type | Description |
|---|---|---|
obj | O | The target object or array (mutated in place) |
path | string | Dot-separated path, e.g. `'user.addresses.0.street'` |
value | unknown | The value to assign at the path |
Returns
OThe same `obj`, for chaining