Array.filter() in JavaScript: keeping only the items that pass a test

filter hands back a new array of the items the function said yes to: the rule of the test, the empty-array case and the choice against find.
3 min read
Believemy logo

Keeping only the paid orders, only the files with one extension, only the active users: the move is so frequent that the language set aside a method for it, and writing a loop for the job no longer holds up.

filter keeps whatever passes a test and drops the rest, without ever touching the original array.


Definition

filter is an Array method that calls a function on each item and hands back a new array made of those the function answered true for. Its length is therefore lower than or equal to the starting one.

JAVASCRIPT
const orders = [
  { id: 1, paid: true, amount: 49 },
  { id: 2, paid: false, amount: 19 },
  { id: 3, paid: true, amount: 99 },
];

const paid = orders.filter((o) => o.paid);
console.log(paid.length);   // 2

const large = orders.filter((o) => o.amount > 500);
console.log(large);         // []

That last line deserves attention: filter always hands back an array, an empty one where needed, never undefined. It can therefore be walked or measured with no check beforehand.


What the function has to return

The return does not have to be a boolean: its Truthy and falsy nature is what decides. An empty string, 0, null and undefined drop the item, everything else keeps it.

JAVASCRIPT
const raw = ["fr", "", null, "en", undefined, "es"];

console.log(raw.filter(Boolean));   // [ 'fr', 'en', 'es' ]

Handing Boolean straight over as the test function is the established idiom for cleaning empty values out of an array. It works because Boolean ignores the second and third arguments filter passes to it.


filter, find or some

What you are afterMethodWhat you receive
Everything that passesfilterAn array
The first that passesArray.find()The item, or undefined
Whether one existsArray.some()A boolean
How many passfilter then lengthA number

The second row is the one most often missed: writing filter(...)[0] walks the whole array for a single item, where find stops at the first hit.


Frequently asked questions

Question

Does filter change the original array?

Never. It builds a new one, holding the same objects rather than copies. Changing a property on a filtered item therefore shows up in both arrays, since it is one object referenced twice.

Question

How do you remove an item by identifier?

By keeping all the others, with list.filter((o) => o.id !== target). That form beats splice, which changes the array in place and requires knowing the exact position.

Question

Does chaining filter and Array.map() cost two passes?

Yes, two passes instead of one. Across a few hundred items, the readability of the chain is well worth that spending. Past several tens of thousands, a single loop or a Array.reduce() becomes defensible.

Related terms

Discover our javaScript glossary

Every word of JavaScript explained simply: keywords, built-in objects, methods, errors and concepts. Clear definitions and examples that actually run, to learn and to troubleshoot.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.