Tutorial

How To Use the filter() Array Method in JavaScript

Updated on August 26, 2021
author

Ceferino IV Villareal

English
How To Use the filter() Array Method in JavaScript

Introduction

The filter() Array method creates a new array with elements that fall under a given criteria from an existing array.

In this article, you will learn about the filter() Array method.

Prerequisites

If you would like to follow along with this article, you will need:

Using filter() on an Array of Numbers

The syntax for filter() resembles:

var newArray = array.filter(function(item) {
  return condition;
});

The item argument is a reference to the current element in the array as filter() checks it against the condition. This is useful for accessing properties, in the case of objects.

If the current item passes the condition, it gets returned to the new array.

Consider this example array of numbers:

var numbers = [1, 3, 6, 8, 11];

Then apply a filter() to return all numbers that are greater than 7:

var greaterThanSeven = numbers.filter(function(number) {
  return number > 7;
});

console.log(greaterThanSeven);

This code will generate a new filtered array:

output
[8, 11]

An array with two values greater than 7 is returned.

Using filter() on an Array of Objects

A common use case of filter() is with an array of objects through their properties.

Consider this example array of creature objects:

var creatures = [
  {name: "Shark", habitat: "Ocean"},
  {name: "Whale", habitat: "Ocean"},
  {name: "Lion", habitat: "Savanna"},
  {name: "Monkey", habitat: "Jungle"}
];

Then apply a filter() to return all creatures with a habitat that is equal to Ocean:

var aquaticCreatures =  creatures.filter(function(creature) {
  return creature.habitat == "Ocean";
});

console.log(aquaticCreatures);

This code will generate a new filtered array:

Output
[ {name: "Shark", habitat: "Ocean"}, {name: "Whale", habitat: "Ocean"} ]

An array with the two creatures that have a habitat in the “Ocean” is returned.

Conclusion

In this article, you learned about the filter() Array method.

For more details, consult the MDN Reference on filter().

Filter is only one of several iteration methods on Arrays in JavaScript, read How To Use Array Iteration Methods in JavaScript to learn about the other methods like map() and reduce().

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products


About the authors
Default avatar
Ceferino IV Villareal

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel