Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Apply a Sequence of Updates

Immutable Statemedium

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

Implement `applyUpdates(state, updates)` that folds an array of partial-state patches onto `state`, returning the new state without mutating the original.

Examples

Input: state { a:1, b:2 }, updates [{ b:3 }, { c:4 }]
Output: result { a:1, b:3, c:4 }, original unchanged
Each patch merges immutably; the original object is never modified.

Sample tests

Input: merges patches, leaves original
Output: [{"a":1,"b":3,"c":4},{"a":1,"b":2}]
Input: later patches win
Output: {"x":3}

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Mutating state with Object.assign(state, patch) changes the caller's object.

Learning resources

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

Folding patches with spread produces a new merged object each step, the immutable batched-update pattern.

Loading...
⌘/Ctrl + Enter

Run your code to see results.