set()
set<
T>(object,path,value):T
Sets the value at path of object, returning a new object (immutable).
We prefer to wrap this method to ensure Immutability and safe property access. See Design Philosophy
Creates intermediate objects/arrays as needed based on path structure.
Type Parametersβ
T: T = anyβ
The type of the input object.
Parametersβ
object: Tβ
The object to set the value in.
path: string | symbol | (string | number | symbol)[]β
Property path (dot notation string, symbol, or array of keys).
value: anyβ
The value to set.
Returns: Tβ
A new object with the value set at the specified path.
See Alsoβ
Sinceβ
2.0.0
Noteβ
Uses structuredClone for performance; falls back to deepClone for symbol keys.
Performanceβ
O(n + m) time & space where n is object size, m is path length. structuredClone when possible (faster), deepClone only for symbol keys. Early returns for null/empty path.
Also known asβ
assocPath (Ramda) Β· set (Lodash, es-toolkit, Remeda, Radashi, Modern Dash) Β· β (Effect, Antfu)
Exampleβ
const obj = { a: { b: { c: 3 } } };
set(obj, 'a.b.c', 42); // => { a: { b: { c: 42 } } }
set(obj, ['a', 'b', 'd'], 'new'); // => { a: { b: { c: 3, d: 'new' } } }
// Create nested structures automatically
set({}, 'user.profile.name', 'John');
// => { user: { profile: { name: 'John' } } }
// Array creation with bracket notation
set({}, 'items[0].value', 42);
// => { items: [{ value: 42 }] }
How it works?β
Immutably sets a value at a nested path, returning a new object.
Immutabilityβ
Auto-Creationβ
Creates intermediate objects/arrays as needed:
Array Creationβ
Use Casesβ
Update nested data safely πβ
Set a value at a deep path, creating intermediate objects if missing. Returns a new object (immutable). Essential for reducer-like state updates.
const updatedState = set(state, 'users[0].isActive', true);
Create deep structuresβ
Build a deeply nested object from scratch by setting a single path on an empty object. Useful for converting flat keys to nested objects.
const obj = set({}, 'a.b.c', 'value');
// { a: { b: { c: 'value' } } }
Append to arraysβ
Use numeric indices in the path to create or update array elements.
const list = set([], '[0].name', 'First');
// [{ name: 'First' }]
Update dynamic form fields by pathβ
Handle onChange events for deeply nested form fields using a dynamic path.
The standard pattern for complex forms with nested objects (address, billing, etc.).
const [formData, setFormData] = useState({
name: "",
address: { street: "", city: "", zip: "" },
billing: { card: "", expiry: "" },
});
const handleChange = (path: string, value: string) => {
setFormData((prev) => set(prev, path, value));
};
// In JSX:
// <input onChange={(e) => handleChange("address.city", e.target.value)} />
// <input onChange={(e) => handleChange("billing.card", e.target.value)} />
Update chart configuration at a specific pathβ
Modify a specific chart option deep in the config tree without touching the rest. Perfect for interactive chart editors and dashboard customization.
let chartConfig = {
title: { text: "Revenue" },
xAxis: { type: "category", labels: { rotation: 0 } },
series: [{ data: [10, 20, 30] }],
};
// User rotates x-axis labels
chartConfig = set(chartConfig, "xAxis.labels.rotation", -45);
// User changes title
chartConfig = set(chartConfig, "title.text", "Monthly Revenue");
renderChart(chartConfig);
Update tree node expanded state by pathβ
Toggle a tree node's expanded state using its path in the tree structure. Essential for tree components with expand/collapse functionality.
let treeState = {
root: {
expanded: true,
children: {
src: { expanded: false, children: {} },
tests: { expanded: false, children: {} },
},
},
};
const toggleNode = (path: string) => {
const currentExpanded = get(treeState, `${path}.expanded`, false);
treeState = set(treeState, `${path}.expanded`, !currentExpanded);
renderTree(treeState);
};
toggleNode("root.children.src");
// => src node is now expanded
Update stepper step statusβ
Mark a step as completed in a multi-step wizard state. Perfect for stepper components tracking progress through a workflow.
let wizardState = {
steps: [
{ label: "Account", status: "current" },
{ label: "Profile", status: "pending" },
{ label: "Review", status: "pending" },
],
};
const completeStep = (stepIndex: number) => {
wizardState = set(wizardState, `steps[${stepIndex}].status`, "completed");
if (stepIndex + 1 < wizardState.steps.length) {
wizardState = set(wizardState, `steps[${stepIndex + 1}].status`, "current");
}
renderStepper(wizardState);
};
completeStep(0);
// => Step 0 is "completed", Step 1 is "current"
Build analytics event payloads dynamicallyβ
Construct nested analytics payloads from flat user interaction data. Essential for tracking systems with structured event schemas.
let event = {};
event = set(event, "action", "purchase");
event = set(event, "metadata.product.id", "SKU-42");
event = set(event, "metadata.product.price", 29.99);
event = set(event, "metadata.user.segment", "premium");
analytics.track(event);
// => { action: "purchase", metadata: { product: { id: "SKU-42", price: 29.99 }, user: { segment: "premium" } } }