Skip to content

pick

Creates a new object containing only the specified keys from the source object. Supports dot notation for selecting nested keys.

import { pick } from "1o1-utils";
import { pick } from "1o1-utils/pick";
function pick<T extends Record<string, unknown>>({ obj, keys }: PickParams<T>): Record<string, unknown>
NameTypeRequiredDescription
objTYesThe source object
keys(keyof T | string)[]YesKeys to include (supports dot notation for nested keys)

Record<string, unknown> — A new object with only the specified keys.

pick({ obj: { a: 1, b: 2, c: 3 }, keys: ["a", "c"] });
// => { a: 1, c: 3 }
// Nested keys with dot notation
pick({ obj: { a: 1, b: { x: 10, y: 20 } }, keys: ["a", "b.x"] });
// => { a: 1, b: { x: 10 } }
  • Throws if obj is not an object.
  • Throws if keys is not an array.
  • Supports nested key selection via dot notation.
  • Missing keys are silently skipped.

select keys, extract properties, subset object, pluck

I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use pick to extract only needed fields from an API response