Underscore.js _.filter() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Collection

Definition

Looks through each value in the list, returning an array of all the values that pass a truth test (predicate). The predicate is transformed through iteratee for shorthand syntaxes (official Underscore docs). Stable Collection helper — not deprecated.

Pass the required arguments shown below. Underscore collection helpers usually return a new value — check whether the source is mutated.

Syntax

Call _.filter. Start with this line.

Underscore
_.filter(list, predicate, [context])
CallWhat it does
_.filter(list, predicate, [context])Primary signature.
_.filter(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.filter(list, predicate, [context])

Parameters

ParameterDescription
listRequired. Array, array-like, or plain object
predicateRequired. Truth test; shorthands via iteratee
contextOptional. Bound as this in the predicate

Return Value

Return ValueDescription
*New array of matching values

Underscore.js Examples

Example 1 — Official Docs Evens

Keep every even number from 1 through 6.

Underscore
var evens = _.filter([1, 2, 3, 4, 5, 6], function (num) {
  return num % 2 == 0;
});

evens;
// → [2, 4, 6]
Try It Yourself

How It Works

  1. Test. Predicate returns true for even numbers.
  2. Keep. 2, 4, and 6 are pushed into a new array.
  3. Skip. Odds are ignored.
  4. Docs sample. This is the official Underscore example.

Example 2 — Matcher & Property Shorthand

Filter objects without writing a custom function.

Underscore
var fruits = [
  { name: "apple", type: "fruit" },
  { name: "carrot", type: "veg" },
  { name: "pear", type: "fruit" }
];

_.filter(fruits, { type: "fruit" });
// → [{ name: "apple", … }, { name: "pear", … }]

_.filter(fruits, "type");
// → all three (each has a truthy type)
Try It Yourself

How It Works

  1. Matcher. { type: "fruit" } keeps objects that match those fields.
  2. Property. "type" keeps items whose type is truthy.
  3. Same idea as where. Matcher filter is close to _.where.
  4. Shorthand. Underscore builds the predicate via iteratee.

Example 3 — _.filter vs _.reject

Same predicate, opposite keep rules.

Underscore
function isEven(n) { return n % 2 == 0; }
var nums = [1, 2, 3, 4, 5, 6];

_.filter(nums, isEven);   // → [2, 4, 6]
_.reject(nums, isEven);   // → [1, 3, 5]
Try It Yourself

How It Works

  1. filter. Keeps values where the predicate is truthy.
  2. reject. Keeps values where the predicate is falsy.
  3. Same test. One isEven function drives both results.
  4. Pick one. Prefer the name that matches your intent (keep vs drop).

Example 4 — Empty Result, Identity & Alias

No matches → []; omit predicate to keep truthy values; _.select matches _.filter.

Underscore
_.filter([1, 2, 3], function (n) { return n > 10; });
// → []

_.filter([0, 1, "", "ok", null, 2]);
// → [1, "ok", 2]   (identity = truthy)

_.select([1, 2, 3, 4], function (n) { return n > 2; });
// → [3, 4]

_.select === _.filter;
// → true
Try It Yourself

How It Works

  1. No hits. Still returns an array — just empty.
  2. Identity. Omitted predicate keeps truthy values only.
  3. Alias. _.select is the same function as _.filter.
  4. Safe chain. Always an array, so further methods stay easy.

Browser & Runtime Support

_.filter (alias _.select) ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.filter()

Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.filter Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.filter(list, predicate, [context]) returns a new array of every value that passes the predicate. Official example: filter [1..6] for even numbers → [2, 4, 6].
Yes. select is an alias of filter. They are the same function.
You get an empty array []. Unlike _.find (which returns undefined), _.filter always returns an array.
_.filter keeps every match and scans the whole list. _.find returns only the first match and can stop early.
They are opposites. filter keeps truthy predicate results; reject keeps falsy ones.
Yes. Pass an object matcher (like _.where) or a property name string. Underscore runs the predicate through iteratee.

Next: _.where()

Filter a list of objects by matching key/value properties in one call.

_.where() tutorial →

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.

10 people found this page helpful