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: {...} }