Lodash _.reject() method
What you’ll learn
- How
_.reject(collection, predicate)omits elements where the predicate is truthy—the inverse offilter. - Iteratee shorthands (functions, paths, partial matches) identical to other Lodash collection APIs.
- Arrays versus plain objects, and when
partitionsaves a second pass. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.filter() first—the predicate means the same thing; only kept versus dropped flips.
- You are comfortable with predicates that return truthy or falsy values.
- You can open Try-it labs or run snippets locally.
Overview
_.reject trims collections by exclusion—drop positives, strip archived rows, or peel outliers—without flipping predicate logic at every call site.
Complement of filter
Truthiness still decides fate—truthy removes an element; falsy keeps it in the new array.
Iteratee toolbox
Lambdas, property strings, and matcher objects work exactly like filter.
Objects included
Reject from keyed records with the same (value, key, collection) callback shape.
Syntax
_.reject(collection, predicate) - collection: array, array-like, or plain object Lodash can iterate.
- predicate: invoked as
(value, index|key, collection); truthy means the value is excluded from the result. - Returns: always an array (possibly empty) of surviving elements or values.
Drop positives from an array
Elements where n > 0 are removed; zero and negatives remain—mirror image of a positivity filter.
import reject from "lodash/reject";
reject([-2, 0, 3, 7], (n) => n > 0);
// → [-2, 0] Remove archived records with a matcher
Shorthand objects reject rows that satisfy the partial match—here, anything with archived: true disappears.
import reject from "lodash/reject";
reject(
[
{ id: 1, role: "admin", archived: false },
{ id: 2, role: "viewer", archived: true },
{ id: 3, role: "editor", archived: false }
],
{ archived: true }
);
// → rows without archived: true Reject large values from a plain object
Keys drive iteration only—the output stays an array of retained values, same as filter.
import reject from "lodash/reject";
reject({ min: 5, mid: 15, max: 40 }, (v) => v >= 10);
// → [5] 📋 _.reject vs filter, partition, negated predicates
| API | Outcome | Best when |
|---|---|---|
_.reject(collection, predicate) | Array without truthy predicate hits | Exclusion reads clearer than wrapping predicates in ! |
_.filter(collection, predicate) | Array of matches | You phrase logic as what to keep |
_.partition(collection, predicate) | [matches, rejects] | You need both buckets in one pass |
array.filter((x) => !pred(x)) | Array without matches | No Lodash iteratee sugar on arrays |
Pitfalls to avoid
Lost keys on objects
Rejecting over an object still yields an array of values—use entries helpers when keys must survive.
Double predicates
Calling both filter and reject on the same collection evaluates the predicate twice—prefer partition.
Ambiguous predicates
JavaScript truthiness can surprise—return explicit booleans when reviewing edge cases.
❓ FAQ
Summary
- Purpose:
_.reject(collection, predicate)returns elements where the predicate is falsy—the inverse selection offilter. - Contrast: use
partitionwhen both keep and drop lists matter in one traversal. - Next: Lodash _.sample(), Lodash _.reduceRight() (previous), or collection hub.
_.reject(collection, predicate) is literally the inverse selection of filter with the same predicate—elements dropped by one appear in the other. Prefer whichever reads clearer and pair with partition when you need both buckets in one pass.
6 people found this page helpful
