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โ
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โ
| pickBy | omitBy | |
|---|---|---|
| Keeps if | predicate = true | predicate = false |
| Logic | Whitelist | Blacklist |
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"