pick()
pick<
T,K>(object,keys):Pick<T,K>
Creates a new object with only the specified keys.
Non-existent keys are ignored.
Type Parametersβ
T: T extends Record<PropertyKey, any>β
The type of the input object.
K: K extends string | number | symbolβ
The type of the keys to pick.
Parametersβ
object: Tβ
The object to pick keys from.
keys: readonly K[]β
Array of keys to pick.
Returns: Pick<T, K>β
A new object with only the specified keys.
See Alsoβ
Sinceβ
2.0.0
Performanceβ
O(k) time & space where k is number of keys to pick. Uses for-of loop and hasOwnProperty check.
Also known asβ
objectPick (Antfu) Β· pick (Lodash, es-toolkit, Remeda, Radashi, Ramda, Effect, Modern Dash)
Exampleβ
const obj = { a: 1, b: 2, c: 3 };
pick(obj, ['a', 'c']); // => { a: 1, c: 3 }
pick(obj, ['b']); // => { b: 2 }
const user = { id: 1, name: 'John', password: 'secret' };
pick(user, ['id', 'name']); // => { id: 1, name: 'John' }
How it works?β
Creates a new object with only the specified keys.
Selection Processβ
pick vs omitβ
| pick | omit | |
|---|---|---|
| Keeps | specified keys | all except specified |
| Use case | whitelist | blacklist |
Use Casesβ
Select specific subsets πβ
Create a shallow copy containing only the specified keys. Critical for creating strict DTOs or filtering API payloads.
const payload = pick(formData, ['username', 'email']);
Extract form subset for validationβ
Select only the fields relevant to a specific validation step. Useful for multi-step forms or partial schema validation.
const step1Data = pick(formData, ['firstName', 'lastName', 'email']);
const isValid = validateStep1Schema(step1Data);
Whitelist allowed parametersβ
Ensure that only permitted keys are passed to a function or API. Security critical for preventing mass assignment vulnerabilities.
const safeInput = pick(req.body, ['title', 'content', 'authorId']);
db.create(safeInput);
Extract safe fields for loggingβ
Pick only non-sensitive fields before writing to logs. Critical for compliance (GDPR, HIPAA) and preventing PII leaks in log systems.
const request = {
userId: "u-123",
endpoint: "/api/checkout",
method: "POST",
creditCard: "4111-1111-1111-1111",
ip: "192.168.1.1",
timestamp: 1703001200,
};
const safeLog = pick(request, ["userId", "endpoint", "method", "timestamp"]);
logger.info("API request", safeLog);
// Logs: { userId: "u-123", endpoint: "/api/checkout", method: "POST", timestamp: 1703001200 }
// Credit card and IP never reach the logs
Build chart dataset from raw recordsβ
Extract only the fields needed for chart rendering from complex records. Essential for transforming API data into chart-compatible formats.
const salesRecords = [
{ id: 1, product: "Widget", revenue: 5000, cost: 2000, date: "2025-01", region: "EU" },
{ id: 2, product: "Gadget", revenue: 8000, cost: 3500, date: "2025-02", region: "US" },
];
const chartData = salesRecords.map((r) => pick(r, ["date", "revenue", "cost"]));
// => [{ date: "2025-01", revenue: 5000, cost: 2000 }, ...]
renderBarChart(chartData);
Extract preset fields for saving user preferencesβ
Pick only the saveable fields from a large settings object. Perfect for saving user presets without storing transient UI state.
const fullSettings = {
theme: "dark",
fontSize: 16,
language: "fr",
sidebarOpen: true,
lastVisitedPage: "/dashboard",
unsavedChanges: true,
};
const presetData = pick(fullSettings, ["theme", "fontSize", "language"]);
// => { theme: "dark", fontSize: 16, language: "fr" }
localStorage.setItem("user-preset", JSON.stringify(presetData));