Pick
JS Functions
Easy
10 min
objects
lodash
Implement pick(object, keys). It returns a new object with only the listed keys copied from object.
A typical use is whitelisting fields before sending data to an API or logging, so that new sensitive fields added later are excluded by default.
const user = { id: 1, name: 'Ada', email: 'ada@example.com', password: 'secret' };
pick(user, ['id', 'name']); // { id: 1, name: 'Ada' }
pick(user, ['id', 'missing']); // { id: 1 }
pick(user, []); // {}
Signature
pick(object, keys) // keys: Array<string>
Requirements
- Only top-level keys. Dotted strings like
'a.b'are treated as a literal key name (lodash'spickwould treat them as deep paths; we don't). - Copy a key only if it is an own property of
object. Missing keys are skipped (not set toundefined). Keys that exist with valueundefinedare copied. - The copy is shallow: nested objects are shared by reference.
- Never mutate
object. - If
objectisnull/undefined, return{}.
Follow-up
- Write the TypeScript signature so
pick(user, ['id'])is typed as{ id: number }. - When would you prefer
pick(allow-list) overomit(deny-list)?
Spotted a mistake or a rights concern? Report an issue