Pick
VerifiedReturns a new object with only the given keys, fully typed.
- object
- pick
Install
npx shadcn@latest add https://quicksnip.dev/r/pick.jsonCode
/** Returns a new object with only the given keys. */
export function pick<T extends object, K extends keyof T>(obj: T, keys: readonly K[]): Pick<T, K> {
const result = {} as Pick<T, K>;
for (const key of keys) {
if (Object.prototype.hasOwnProperty.call(obj, key)) result[key] = obj[key];
}
return result;
}Usage
pick({ id: 1, name: "Ada", password: "x" }, ["id", "name"]); // { id: 1, name: "Ada" }