Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Toggle Checkbox by Id

Events & Formsmedium

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

Implement `toggleChecked(list, id)` returning a new list with the `checked` flag flipped on the item whose `id` matches.

Sample tests

Input: toggleChecked([{"id":1,"checked":false}], 1)
Output: [{"id":1,"checked":true}]
Input: toggleChecked([{"id":1,"checked":true},{"id":2,"checked":false}], 2)
Output: [{"id":1,"checked":true},{"id":2,"checked":true}]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Mutating the matched item.
  • Toggling every item instead of the one with the id.

Learning resources

  • React: Reacting to Input with State
Approach & explanation (try first)

map returns a new list where only the item with the given id gets a new object with checked flipped, leaving the rest by reference.

Loading...
⌘/Ctrl + Enter

Run your code to see results.