Skip to content

pick

Creates a new object with only the specified keys.

Available since v2.0.0

import { pick } from '@helpers4/object';
pick<T extends Record<string, unknown>, K extends string | number | symbol>(obj: T, keys: readonly K[]): Pick<T, K>
ParameterTypeDescription
objTThe source object
keysreadonly K[]The keys to pick

Pick<T, K> — A new object with only the picked keys

Creates a new object with only the specified keys.

pick({ a: 1, b: 2, c: 3 }, ['a', 'c'])
// => { a: 1, c: 3 }

Useful to select only the fields you need from an object.

const user = { id: 1, name: 'Alice', email: 'alice@example.com', password: 'secret' };
pick(user, ['id', 'name', 'email'])
// => { id: 1, name: 'Alice', email: 'alice@example.com' }

View source on GitHub