Skip to content

Function.update

github-actions[bot] edited this page Nov 18, 2026 · 13 revisions

umt / update

Function: update()

update<T>(object, path, updater): T

Defined in: Object/update.ts:29

Sets a value at a deeply nested path from its current value, mutating the object. Reads the current value like get and writes the result like set.

The updater receives undefined when the path is missing. Intermediate segments that are missing or not objects are replaced with new objects. An empty path array leaves the object unchanged and does not call the updater.

Type Parameters

T

T extends object

Type of the source object

Parameters

object

T

Target object (mutated)

path

string | string[]

Dot-separated string or path segments array

updater

(value) => unknown

Computes the new value from the current value

Returns

T

The same object reference, for chaining

Remarks

Prototype pollution warning: This function does not filter out prototype-polluting keys (__proto__, constructor, prototype). Sanitize user-controlled paths before calling.

Example

update({ a: { b: 1 } }, "a.b", (value) => (value as number) + 1); // { a: { b: 2 } }
update({}, ["a", "b"], (value) => value ?? 0); // { a: { b: 0 } }

API

Classes

Interfaces

Type Aliases

Variables

Functions

Clone this wiki locally