Pick only the keys you need
An endpoint returns a user object with forty fields. Your avatar component needs three. Passing the whole thing wastes memory in React state and makes every re-render comparison slower than it should be.
Implement pick(source, keys). source is a plain object and keys is an array of property names. Return a new object holding only those keys that are own properties of source, with the same values. A requested key that source does not have is skipped entirely — it must not appear in the result at all. The result's keys appear in the order given in the keys array, not the order they sit in source. An empty keys array returns an empty object. Never modify source.
What it has to do
- Copy across only the requested keys that
sourceactually owns. - Skip absent keys entirely rather than setting them to
undefined. - Order the result's keys to match the order of the
keysarray. - Return
{}whenkeysis empty. - Do not mutate
source.
Your workspace
Ready to check it?
5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.
AI Crack & Solution Assist
Stuck? Get instant AI hints or break down the optimal solution.
Stuck? The javascript course covers everything this challenge needs.