set
Sets a nested value on an object using a dot-notation path. Non-mutating: returns a new object with only the nodes on the path cloned (structural sharing).
Import
Section titled “Import”import { set } from "1o1-utils";import { set } from "1o1-utils/set";Signature
Section titled “Signature”function set<T extends Record<string, unknown>>({ obj, path, value, objectify }: SetParams<T>): Record<string, unknown>Parameters
Section titled “Parameters”| Name | Type | Required | Description |
|---|---|---|---|
obj | T | Yes | The source object |
path | string | Yes | Dot-notation path (e.g. "address.city") |
value | unknown | Yes | The value to write at path |
objectify | boolean | No | When true, numeric segments create objects with string keys instead of arrays (default false) |
Returns
Section titled “Returns”Record<string, unknown> — A new object with value set at path. Input is never mutated.
Examples
Section titled “Examples”set({ obj: { a: { b: 1 } }, path: "a.b", value: 2 });// => { a: { b: 2 } }
// Numeric segments create arrays by defaultset({ obj: {}, path: "items.0.name", value: "x" });// => { items: [{ name: "x" }] }
// Force plain objects with objectifyset({ obj: {}, path: "items.0.name", value: "x", objectify: true });// => { items: { "0": { name: "x" } } }Edge Cases
Section titled “Edge Cases”- Throws if
objis not an object,pathis not a string, orpathis empty. - Does not mutate the input — only nodes on the path are cloned; siblings keep their reference.
- Creates missing intermediate containers: arrays when the next segment is numeric (default), objects otherwise. Set
objectify: trueto always create objects. - Overwrites intermediate primitives (
null, strings, numbers) with fresh objects/arrays.
Also known as
Section titled “Also known as”write nested, dot notation, deep set, property path, immutable set, lodash set
Prompt suggestion
Section titled “Prompt suggestion”I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use set to immutably update a deeply nested field in a Redux-style state