pickBy()
pickBy<
T>(object,predicate):Partial<T>
Creates a new object with properties that satisfy the predicate.
Only enumerable string keys are included (Symbol keys are ignored).
Type Parametersβ
T: T extends Record<PropertyKey, any>β
The type of the input object.
Parametersβ
object: Tβ
The object to pick properties from.
predicate: (value, key, object) => booleanβ
Function to test each property: (value, key, object) => boolean.
Returns: Partial<T>β
A new object with only the properties that pass the predicate.
See Alsoβ
Sinceβ
2.0.0
Performanceβ
O(n) time & space where n is number of properties.
Also known asβ
filter (Effect) Β· filterKey (Radashi) Β· pickBy (Lodash, es-toolkit, Remeda, Ramda) Β· β (Modern Dash, Antfu)
Exampleβ
const obj = { a: 1, b: 'hello', c: 3, d: null };
// Pick by value type
pickBy(obj, (v) => typeof v === 'string');
// => { b: 'hello' }
// Pick by key pattern
pickBy(obj, (_, key) => String(key).startsWith('a'));
// => { a: 1 }
// Pick non-null values
pickBy(obj, (v) => v != null);
// => { a: 1, b: 'hello', c: 3 }
How it works?β
Creates a new object with properties that satisfy a predicate.
Filter by Typeβ
Filter Non-Nullβ
pick vs pickByβ
| pick | pickBy | |
|---|---|---|
| Selects by | Key names | Predicate function |
| Use case | Known keys | Dynamic filtering |
Use Casesβ
Filter properties dynamically πβ
Create a new object with properties that satisfy a predicate function. Useful for cleaning objects by value.
const validOnly = pickBy(data, (val) => isValid(val));
Extract numeric properties onlyβ
Filter an object to keep only properties with numeric values. Useful for separating metrics from metadata in mixed objects.
const metrics = pickBy(stats, (value) => typeof value === 'number');
// { views: 1200, clicks: 89 } β excludes string/boolean fields
Remove null or undefined values πβ
Clean up an object by removing empty properties. Essential for generating clean query parameters or JSON payloads.
const cleanObj = pickBy(data, (value) => value != null);
Filter available venue sections for event bookingβ
Filter venue sections to show only available areas during ticket sales. Essential for event booking platforms managing real-time seat availability.
const venueSection = {
vipBox: { available: 0, price: 500, capacity: 20 },
orchestra: { available: 45, price: 200, capacity: 150 },
mezzanine: { available: 0, price: 150, capacity: 200 },
balcony: { available: 89, price: 80, capacity: 300 },
standing: { available: 150, price: 50, capacity: 500 },
};
// Show only sections with available seats
const availableSections = pickBy(venueSection, (section) => section.available > 0);
// => { orchestra: {...}, balcony: {...}, standing: {...} }
// Filter premium sections for VIP customers
const premiumSections = pickBy(venueSection, (sec) => sec.price >= 150);
// => { vipBox: {...}, orchestra: {...}, mezzanine: {...} }
// Find sections with bulk availability for group bookings
const groupFriendly = pickBy(venueSection, (sec) => sec.available >= 20);
// => { orchestra: {...}, balcony: {...}, standing: {...} }
Filter visible table columns by user preferenceβ
Pick only the columns the user has chosen to display in a data table. Essential for data table components with column visibility toggles.
const allColumns = {
name: { label: "Name", visible: true, width: 200 },
email: { label: "Email", visible: true, width: 250 },
role: { label: "Role", visible: false, width: 120 },
status: { label: "Status", visible: true, width: 100 },
lastLogin: { label: "Last Login", visible: false, width: 180 },
createdAt: { label: "Created", visible: false, width: 150 },
};
const visibleColumns = pickBy(allColumns, (col) => col.visible);
// => { name: {...}, email: {...}, status: {...} }
renderTableHeaders(Object.values(visibleColumns));
Extract enabled feature flagsβ
Pick only the enabled features from a feature flag configuration. Essential for feature flag systems and conditional rendering.
const featureFlags = {
darkMode: true,
newDashboard: false,
betaSearch: true,
legacyNav: false,
aiAssistant: true,
};
const enabledFeatures = pickBy(featureFlags, (enabled) => enabled);
// => { darkMode: true, betaSearch: true, aiAssistant: true }
const featureList = Object.keys(enabledFeatures);
// => ["darkMode", "betaSearch", "aiAssistant"]
Filter chart series with dataβ
Keep only chart series that have actual data points to display. Perfect for dynamic dashboards where some metrics may be empty.
const chartSeries = {
revenue: [100, 200, 300],
expenses: [80, 150, 250],
refunds: [],
taxes: [10, 20, 30],
bonuses: [],
};
const activeSeries = pickBy(chartSeries, (values) => values.length > 0);
// => { revenue: [...], expenses: [...], taxes: [...] }
renderMultiLineChart(activeSeries);