Lodash _.filter() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

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 collection is an object versus an array—and how that differs from native filter.
  • 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

javascript
_.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.
1

Keep positives from an array

Classic numeric filtering mirrors native semantics while staying compatible with chain wrappers.

javascript
import filter from "lodash/filter";

filter([-2, 0, 3, 7], (n) => n > 0);
// → [3, 7]
Try it Yourself
2

Records with a matcher iteratee

Partial object shorthand keeps rows whose shape satisfies the template—ideal for flag-heavy DTOs.

javascript
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
Try it Yourself
3

Filter values from a plain object

Keys are used only while iterating—the returned structure is still an array of winning values.

javascript
import filter from "lodash/filter";

filter({ min: 5, mid: 15, max: 40 }, (v) => v >= 10);
// → [15, 40]
Try it Yourself

📋 _.filter vs reject, partition, native filter

APIOutcomeBest when
_.filter(collection, predicate)Array of matchesKeep passing rows or values
_.reject(collection, predicate)Array of non-matchesPrefer complementary predicate without negating manually
_.partition(collection, predicate)[matches, rejects]You need both slices in one pass
array.filter(predicate)Array of matchesPure arrays without Lodash iteratees

Pitfalls to avoid

Shape

Lost keys on objects

Filtering an object yields values only—plan an entries round-trip when keys matter downstream.

Perf

Repeated scans

Chaining multiple full-collection filters costs extra passes—fuse predicates or reach for a single reduce when hot.

Truthy

Ambiguous predicates

Coercion surprises mirror JavaScript truthiness—return explicit booleans when reviewing edge cases.

❓ FAQ

No. Lodash allocates a new array of matches; the original array or object is left unchanged.
An array of values that passed the predicate—iteration order follows Lodash rules for plain objects; keys are not kept in the result shape.
reject keeps elements where the predicate is falsy—the complement of filter with the same iteratee.
Native filter only works on arrays; Lodash filter additionally accepts objects and Lodash iteratee shorthands.
Yes—partial objects, property names, and path arrays follow the same iteratee conventions as map or every.

Summary

  • Purpose: _.filter(collection, predicate) returns a new array of truthy predicate hits.
  • Contrast: use _.partition when both buckets matter; _.reject for the inverted selection.
  • Next: Lodash _.find(), Lodash _.every() (previous), or collection hub.
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful