R
fn

set

v0.0.10tested

Write 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 Collection

Parameters

ParameterTypeDescription
objOThe target object or array (mutated in place)
pathstringDot-separated path, e.g. `'user.addresses.0.street'`
valueunknownThe value to assign at the path

Returns

OThe same `obj`, for chaining