Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Reset Selected Fields

Immutable Statemedium

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

Implement `resetFields(state, keys, value)` returning a new object where each key in `keys` is set to `value`.

Sample tests

Input: resetFields({"a":1,"b":2,"c":3}, ["a","b"], 0)
Output: {"a":0,"b":0,"c":3}
Input: resetFields({"x":5}, [], 0)
Output: {"x":5}

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Mutating the original state while resetting fields.

Learning resources

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

Make one copy with { ...state } and set every key in the list to the reset value, so unaffected fields stay as they were.

Loading...
⌘/Ctrl + Enter

Run your code to see results.