Lodash _.filter() method
What you’ll learn
- How
_.filter(collection, predicate)clones matching entries into a new array. - Iteratee shorthands (functions, paths, partial matches) consistent with other Lodash collection APIs.
- Behavior when
collectionis an object versus an array—and how that differs from nativefilter. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
See _.every() for predicate conventions that pair naturally with inclusion checks.
- You are comfortable with predicates that return truthy or falsy values.
- You can open Try-it labs or run snippets locally.
Overview
_.filter is the workhorse for slicing collections—keep active users, strip nullish placeholders, or isolate rows before mapping—all without touching the source structure.
Non-destructive
Outputs a fresh array even when every element matches—inputs remain intact for retries.
Iteratee toolbox
Swap between lambdas, property strings, and matcher objects without changing call sites.
Objects included
Filter keyed records—not just arrays—using the same predicate signature.
Syntax
_.filter(collection, predicate) - collection: array, array-like, or plain object Lodash can iterate.
- predicate: invoked as
(value, index|key, collection); truthy keeps the value in the result array. - Returns: always an array (possibly empty) of elements or values that matched.
Keep positives from an array
Classic numeric filtering mirrors native semantics while staying compatible with chain wrappers.
import filter from "lodash/filter";
filter([-2, 0, 3, 7], (n) => n > 0);
// → [3, 7] Records with a matcher iteratee
Partial object shorthand keeps rows whose shape satisfies the template—ideal for flag-heavy DTOs.
import filter from "lodash/filter";
filter(
[
{ id: 1, role: "admin", archived: false },
{ id: 2, role: "viewer", archived: true },
{ id: 3, role: "editor", archived: false }
],
{ archived: false }
);
// → first and third rows Filter values from a plain object
Keys are used only while iterating—the returned structure is still an array of winning values.
import filter from "lodash/filter";
filter({ min: 5, mid: 15, max: 40 }, (v) => v >= 10);
// → [15, 40] 📋 _.filter vs reject, partition, native filter
| API | Outcome | Best when |
|---|---|---|
_.filter(collection, predicate) | Array of matches | Keep passing rows or values |
_.reject(collection, predicate) | Array of non-matches | Prefer complementary predicate without negating manually |
_.partition(collection, predicate) | [matches, rejects] | You need both slices in one pass |
array.filter(predicate) | Array of matches | Pure arrays without Lodash iteratees |
Pitfalls to avoid
Lost keys on objects
Filtering an object yields values only—plan an entries round-trip when keys matter downstream.
Repeated scans
Chaining multiple full-collection filters costs extra passes—fuse predicates or reach for a single reduce when hot.
Ambiguous predicates
Coercion surprises mirror JavaScript truthiness—return explicit booleans when reviewing edge cases.
❓ FAQ
Summary
- Purpose:
_.filter(collection, predicate)returns a new array of truthy predicate hits. - Contrast: use
_.partitionwhen both buckets matter;_.rejectfor the inverted selection. - Next: Lodash _.find(), Lodash _.every() (previous), or collection hub.
When collection is a plain object, _.filter still returns an array of matching values—not an object with keys preserved. Reach for _.pickBy, hand-built reducers, or Object.entries plus fromPairs when you need key-aware results.
6 people found this page helpful
