reject()
reject<
T>(collection,predicate):T[]
reject<
T>(collection,predicate):Partial<T>
The opposite of filter; this method returns the elements of collection that predicate does not return truthy for.
๐ Why is this a Hidden Gem?
The inverse of filter โ exclude items instead of selecting them.
Use array.filter() with negated predicate or Object.entries().filter() directly instead.
Type Parametersโ
T: Tโ
The type of elements in the array, or the object type.
Parametersโ
Overload 1:
collection: T[]โ
The collection to iterate over.
predicate: (value, index, array) => booleanโ
The function invoked per iteration.
Overload 2:
collection: Tโ
The collection to iterate over.
predicate: (value, key, object) => booleanโ
The function invoked per iteration.
Returns: T[]โ
The new filtered array or object.
See Alsoโ
Sinceโ
2.0.0
Also known asโ
reject (Lodash, es-toolkit, Remeda, Ramda) ยท โ (Radashi, Effect, Modern Dash, Antfu)
Exampleโ
const numbers = [1, 2, 3, 4, 5];
// โ Deprecated approach
const oddNumbers = reject(numbers, n => n % 2 === 0);
console.log(oddNumbers); // [1, 3, 5]
// โ
Recommended approach (for arrays)
const oddNumbersNative = numbers.filter(n => n % 2 !== 0);
console.log(oddNumbersNative); // [1, 3, 5]
// โ
Recommended approach (for objects)
const obj = { a: 1, b: 2, c: 3 };
const oddValuesObj = Object.fromEntries(
Object.entries(obj).filter(([, value]) => value % 2 !== 0)
);
console.log(oddValuesObj); // { a: 1, c: 3 }
How it works?โ
Returns elements that do NOT pass the predicate (opposite of filter).
Deprecated: Use array.filter() with negated predicate.
Native Equivalentโ
// โ reject(arr, predicate)
// โ
arr.filter(x => !predicate(x))
Use Casesโ
Exclude items matching criteria ๐โ
Filter out elements that match a condition.
const items = [{ active: true }, { active: false }, { active: true }];
items.filter(item => !item.active);
// => [{ active: false }]
Remove invalid entriesโ
Exclude items that fail validation.
const users = [{ email: "" }, { email: "a@b.com" }, { email: null }];
users.filter(u => u.email);
// => [{ email: "a@b.com" }]
Filter out completed tasksโ
Get only pending tasks by excluding completed ones.
const tasks = [{ done: true }, { done: false }, { done: false }];
tasks.filter(t => !t.done);
// => [{ done: false }, { done: false }]