Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Pick Props

Props & Classesmedium

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

Implement `pickProps(props, keys)` returning a new object with only the given keys that exist on `props`, in the order of `keys`.

Sample tests

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

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Copying keys that do not exist, adding undefined values.
  • Mutating the source props object.

Learning resources

  • React: Passing Props to a Component
Approach & explanation (try first)

Walking the keys array and copying only existing props builds a subset object in the requested order, without touching the original.

Loading...
⌘/Ctrl + Enter

Run your code to see results.