Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Merge State

Immutable Stateeasy

Course · Section 9: Data Structures, Modern Operators and Strings · Lecture 111: The Spread Operator (...)

Implement `mergeState(state, patch)` returning a new object combining both, with `patch` taking precedence.

Sample tests

Input: mergeState({"a":1,"b":2}, {"b":3})
Output: {"a":1,"b":3}
Input: mergeState({}, {"x":1})
Output: {"x":1}

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Object.assign(state, patch) mutates state; assign into a fresh object or use spread.

Learning resources

  • React: Updating Objects in State
Approach & explanation (try first)

Spreading state then patch produces a new object where patch keys override state keys, the standard shallow merge for setState updates.

Loading...
⌘/Ctrl + Enter

Run your code to see results.