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 }]