Aller au contenu principal

omitBy()

omitBy<T>(object, predicate): Partial<T>

Creates an object composed of properties that predicate doesn't return truthy for.


Type Parameters

T: T extends Record<string, unknown>

The type of the object.


Parameters

object: T

The source object.

predicate: (value, key) => boolean

The function invoked per property.


Returns: Partial<T>

A new object with filtered properties.


See Also

pickBy


Since

2.0.0


Performance

O(n) where n is the number of own enumerable properties.


Also known as

filter (Effect) · filterKey (Radashi) · omitBy (Lodash, es-toolkit, Remeda) · ❌ (Ramda, Modern Dash, Antfu)


Example

const object = { a: 1, b: '2', c: 3 };

omitBy(object, value => typeof value === 'number');
// => { b: '2' }

// Remove null/undefined values
omitBy({ a: 1, b: null, c: undefined, d: 4 }, v => v == null);
// => { a: 1, d: 4 }

// Remove empty strings
omitBy({ name: 'John', email: '', phone: '123' }, v => v === '');
// => { name: 'John', phone: '123' }

How it works?

Creates a new object excluding properties that satisfy a predicate.

Remove Null Values

Remove Empty Strings

pickBy vs omitBy

pickByomitBy
Keeps ifpredicate = truepredicate = false
LogicWhitelistBlacklist

Use Cases

Remove Null/Undefined Values from objects 📌

Remove null and undefined values from objects. Essential for cleaning data before API calls.

import { omitBy } from "pithos/arkhe/object/omit-by";

const formData = {
name: "John",
email: "john@example.com",
phone: null,
address: undefined,
age: 0,
bio: "",
};

// Remove null and undefined values
const cleanData = omitBy(formData, (value) => value === null || value === undefined);
console.log(cleanData);
// { name: "John", email: "john@example.com", age: 0, bio: "" }

// Note: 0 and "" are preserved (only null/undefined removed)

Filter Sensitive Data before logging 📌

Remove sensitive fields from objects before logging. Critical for security compliance.

import { omitBy } from "pithos/arkhe/object/omit-by";

const sensitiveKeys = new Set(["password", "token", "secret", "apiKey", "creditCard"]);

function sanitizeForLogging(data: Record<string, unknown>) {
return omitBy(data, (_, key) => sensitiveKeys.has(key));
}

const userData = {
id: "123",
email: "user@example.com",
password: "secret123",
token: "jwt-token-here",
preferences: { theme: "dark" },
};

console.log("User action:", sanitizeForLogging(userData));
// { id: "123", email: "user@example.com", preferences: { theme: "dark" } }

Remove Empty Values for query parameters

Filter out empty values when building query strings. Important for clean API requests.

import { omitBy } from "pithos/arkhe/object/omit-by";

function buildQueryString(params: Record<string, unknown>): string {
// Remove empty values
const cleanParams = omitBy(
params,
(value) => value === null || value === undefined || value === ""
);

return new URLSearchParams(
Object.entries(cleanParams).map(([k, v]) => [k, String(v)])
).toString();
}

const searchParams = {
query: "typescript",
page: 1,
limit: 10,
category: "",
author: null,
sortBy: undefined,
};

const queryString = buildQueryString(searchParams);
console.log(queryString); // "query=typescript&page=1&limit=10"