pickBy

Creates a new object by picking properties from the original object where the predicate function returns true. Supports both string and symbol keys.

Usage Scenarios

  • Data Filtering: Select only properties that meet certain criteria.
  • Conditional Selection: Include properties based on their values or keys.
  • Immutable Updates: Create new objects with only specific properties.

Examples

import { pickBy } from 'funtool';

// Pick truthy values
const user = { name: 'Alice', age: 25, isActive: false };
const activeProps = pickBy(user, (value) => Boolean(value));
// activeProps: { name: 'Alice', age: 25 }

// Pick properties with string keys
const sym = Symbol('id');
const obj = { name: 'Bob', [sym]: 123 };
const stringProps = pickBy(obj, (_, key) => typeof key === 'string');
// stringProps: { name: 'Bob' }

Signature

function pickBy<T extends object>(
  obj: T,
  predicate: (value: T[keyof T], key: keyof T) => boolean
): Partial<T>

Parameters

  • obj (T extends object): The source object to process.
  • predicate ((value: T[keyof T], key: keyof T) => boolean): Function that determines which properties to pick.

Returns

  • (Partial<T>): A new object with only the picked properties.