Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Remove by Id

Immutable Arraysmedium

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

Implement `removeById(list, id)` returning a new array without the item whose `id` matches.

Sample tests

Input: removeById([{"id":1},{"id":2}], 1)
Output: [{"id":2}]
Input: removeById([{"id":1}], 2)
Output: [{"id":1}]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • indexOf followed by splice mutates the array and is fragile with object identity.
  • Comparing whole objects by reference instead of comparing the id field.

Learning resources

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

Identifying rows by a stable id is the React-friendly approach (it also gives you good keys). filter keeps every item except the one whose id matches, returning a new array.

Loading...
⌘/Ctrl + Enter

Run your code to see results.