Pick Object Keys

EasyUtility Functions10 min4 tests

Implement a pick function that creates a new object with only the specified keys from the source object.

Requirements

  • Return a new object containing only the listed keys
  • Ignore keys that don't exist on the source object
  • Do not mutate the original object

Example

pick({ a: 1, b: 2, c: 3 }, ['a', 'c']); // { a: 1, c: 3 }
pick({ x: 10, y: 20 }, ['x', 'z']);      // { x: 10 }
Hints (4)
  1. Create a new empty object to hold the result.
  2. Iterate over the keys array, not the object.
  3. Use the in operator to check if a key exists on the object.
  4. Assign matching key-value pairs to the result object.

Topics

  • Utility Functions

Asked at

Amazon · Uber · Stripe

Join Us
blur