Object Update

语言

Reducers and state containers often need to update a deeply nested field without mutating the existing state. Writing the full object-spread chain by hand works, but it quickly becomes noisy.

An easy way to update an object while leaving the original untouched would be to deeply clone it, but that uses much more memory because each update creates an entirely new object, which is wasteful when most values in the original object usually stay the same.

Implement a function objectUpdate(source, path, value) that updates a nested object path in an immutable fashion and returns a new object instance while sharing the original inner structure where possible.

This question is intentionally scoped:

  • source is a plain JavaScript object.
  • path is a non-empty dot-delimited string such as 'draft.user.name'.
  • Every intermediate object already exists.
  • Arrays are out of scope.

Return a new object where the value at path has been replaced with value.

The important constraint is structural sharing:

  • Clone only the objects along the touched path.
  • Keep all untouched branches as the exact same references.
  • Do not mutate the input object.

Examples

const state = {
draft: {
user: {
name: 'Alice',
role: 'admin',
},
meta: {
saved: false,
},
},
theme: {
mode: 'dark',
},
};
const next = objectUpdate(state, 'draft.user.name', 'Bob');
next.draft.user.name; // 'Bob'
next !== state; // true
next.draft !== state.draft; // true
next.draft.user !== state.draft.user; // true
next.draft.meta === state.draft.meta; // true
next.theme === state.theme; // true
const state = {
status: 'draft',
draft: {
title: 'Object Update',
},
};
const next = objectUpdate(state, 'status', 'published');
console.log(next); // { status: 'published', draft: { title: 'Object Update' } }
console.log(state.status); // 'draft'

Arguments

objectUpdate(source, path, value) accepts the following arguments:

ArgumentTypeDescription
sourceObjectThe plain object to update.
pathstringA non-empty dot-delimited path pointing to an existing nested property.
valueunknownThe new value to store at the final path segment.

Returns

Returns a new object with the updated value written at path.

Notes

  • The returned object should reuse untouched nested references from source.
  • Only the objects on the updated path need to be shallow-cloned.
  • You do not need to support updater callbacks, arrays, missing-branch creation, or path validation.

Follow up

Implement Object Update II to support arrays, updater callbacks, and creating missing object branches.

加载编辑器

    Object Update | 带有解决方案的 JavaScript 面试问题