Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Pick Keys

Data Structures, Operators & Stringsmedium

Course · Section 9: Data Structures, Modern Operators and Strings · Lecture 118: Enhanced Object Literals

Implement `pick(obj, keys)` returning a new object with only the entries whose key is in `keys` and present in `obj`, in the order given by `keys`.

Sample tests

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

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Copying keys that are absent adds undefined entries.
  • Iterating the object instead of the keys loses the requested order.

Learning resources

  • MDN: Object
Approach & explanation (try first)

Iterating the keys array and copying present properties yields a subset in the requested order.

Loading...
⌘/Ctrl + Enter

Run your code to see results.