pickBy & omitBy

The pickBy and omitBy utility functions allow you to create new objects by selecting or excluding properties based on a predicate function. pickBy creates an object composed of properties that satisfy a given condition, while omitBy is the opposite, creating an object composed of properties that do not satisfy the condition.

Examples

Example 1: pickBy

Create an object composed of object properties that satisfy a given condition:

import {pickBy} from 'ngx-lift';

const inputObject = {
  name: 'John',
  age: 25,
  city: 'New York',
  isActive: true,
};

const predicate = (value: unknown, key: string) => typeof value === 'string' || key === 'isActive';

const result = pickBy(inputObject, predicate);

// Output:
// {
//   name: 'John',
//   city: 'New York',
//   isActive: true,
// }

Example 2: omitBy

omitBy is the opposite of pickBy, creating an object composed of properties that do not satisfy the given condition.

import {omitBy} from 'ngx-lift';

const inputObject = {
  name: 'John',
  age: 25,
  city: 'New York',
  isActive: true,
};

const predicate = (value: unknown, key: string) => typeof value === 'string' || key === 'isActive';

const result = omitBy(inputObject, predicate);

// Output:
// {
//   age: 25,
// }

API Reference

pickBy

Creates an object composed of object properties that satisfy a given condition.

Signature

pickBy<T>(object: Record<string, T>, predicate: (value: T, key: string) => boolean): Record<string, T>

Parameters

  • object: Record<string, T>

    The source object to pick properties from.

  • predicate: (value: T, key: string) => boolean

    A function that determines which properties to include. Returns true to include the property, false to exclude it.

Returns

A new object containing only the properties that satisfy the predicate.

omitBy

Creates an object composed of object properties that do not satisfy a given condition.

Signature

omitBy<T>(object: Record<string, T>, predicate: (value: T, key: string) => boolean): Record<string, T>

Parameters

  • object: Record<string, T>

    The source object to omit properties from.

  • predicate: (value: T, key: string) => boolean

    A function that determines which properties to exclude. Returns true to exclude the property, false to include it.

Returns

A new object containing only the properties that do not satisfy the predicate.