Skip to content

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 { set } from "1o1-utils";
import { set } from "1o1-utils/set";
function set<T extends Record<string, unknown>>({ obj, path, value, objectify }: SetParams<T>): Record<string, unknown>
NameTypeRequiredDescription
objTYesThe source object
pathstringYesDot-notation path (e.g. "address.city")
valueunknownYesThe value to write at path
objectifybooleanNoWhen true, numeric segments create objects with string keys instead of arrays (default false)

Record<string, unknown> — A new object with value set at path. Input is never mutated.

set({ obj: { a: { b: 1 } }, path: "a.b", value: 2 });
// => { a: { b: 2 } }
// Numeric segments create arrays by default
set({ obj: {}, path: "items.0.name", value: "x" });
// => { items: [{ name: "x" }] }
// Force plain objects with objectify
set({ obj: {}, path: "items.0.name", value: "x", objectify: true });
// => { items: { "0": { name: "x" } } }
  • Throws if obj is not an object, path is not a string, or path is 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: true to always create objects.
  • Overwrites intermediate primitives (null, strings, numbers) with fresh objects/arrays.

write nested, dot notation, deep set, property path, immutable set, lodash set

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