ArrowLeft Icon

Learn JavaScript Array .filter() method with code examples

šŸ“† Ā· ā³ 3 min read Ā· šŸ‘€
Ā·

Introduction

In the previous blog, we learned about the .map() method. If you haven’t read it yet then you can check that out here. In this one, we will learn about the .filter() method. So let’s get started with it.

.Filter()

So you have certain items in an array and you want to filter out a few of them?

Well as the name suggests filter function can be used for this.

MDN defines .filter() as

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

Let’s understand it a little better with an example.

I’m considering the same persons array which we used in the previous blog.

const persons = [
  {
    _id: '001',
    name: 'John Doe',
    age: 25,
  },
  {
    _id: '002',
    name: 'Kevin Smith',
    age: 22,
  },
  {
    _id: '003',
    name: 'Jane Doe',
    age: 17,
  },
  {
    _id: '004',
    name: 'John Wick',
    age: 20,
  },
];

Let’s say we have a task to allow only valid users to vote. So any user whose age is < 18 should not be allowed i.e they should be filtered out.

To do this we can simply use the .filter() method and you’ll see how easy your life can get with the help of such powerful function.

const personsWhoCanVote = persons.filter(function (person) {
  if (person.age >= 18) {
    return true;
  } else {
    return false;
  }
});

// Output of personsWhoCanVote
/*
personsWhoCanVote = [
  {
    _id: '001',
    name: 'John Doe',
    age: 25
  },
  {
    _id: '002',
    name: 'Kevin Smith',
    age: 22
  },
  {
    _id: '004',
    name: 'John Wick',
    age: 20
  }
]
*/

In this case, Jane Doe would get filtered out because her age is 17 which is less than 18.

Basically, if the callback function returns true, the current element will be in the resulting array but if it returns false, then it won’t be.

Also just like .map(), the filter method also takes the same three parameters in the callback function which are currentValue, index and the originalArray and it also returns a new array instead of mutating or modifying the original array.

So this is how the .filter() method works in a nutshell.

But wait!! I know what you are waiting for…

A visual depiction of what is being written about

We know how to use the filter method but its the clean code time!

Let’s refactor our code for valid voters problem

We’ll start with a one-liner solution using filter (because why not!)

const personsWhoCanVote = persons.filter((person) => person.age >= 18);
A visual depiction of what is being written about

And this code will produce the same output as above since we are explicitly returning a boolean value at person.age >= 18 we can simply return this value rather than having an if-else block or even a ternary operator.

Finally, we can make it more clean and maintainable by splitting the logic into its own function for reusability.

const canVote = (person) => person.age >= 18;

const personsWhoCanVote = persons.filter(canVote);

And that’s it.

Remember you don’t necessarily have to follow like this. for eg splitting into different functions or using arrow functions.

This is simply a cherry on the top of the cake.

Next would be .reduce() method. See you there. šŸ‘‹

Important Links

EnvelopeOpen IconStay up to date

Get notified when I publish something new, and unsubscribe at any time.

You may also like

  • # javascript

    Integration Testing in JavaScript with Jest and Nock: A Beginner's Guide

    Integration testing is an essential part of the software development process, and it ensures that all the components of your application work together as expected. In this article, we'll explore the basics of integration testing in JavaScript and show you how to use Jest and Nock to write effective tests that simulate real-world scenarios.

  • # javascript

    Unit Testing in JavaScript: How to Ensure Code Quality and Catch Bugs Early

    Unit testing is a critical aspect of software development that ensures code quality, improves maintainability, and catches bugs early. In this article, we'll explore the basics of unit testing in JavaScript, including what it is, why it's important, and how to write effective unit tests using popular testing frameworks.

  • # javascript

    Array and Object Manipulation in JavaScript: Advanced Techniques

    Arrays and objects are fundamental data structures in JavaScript. They are used extensively in modern web development to store and manipulate data. In this blog post, we will explore advanced techniques for manipulating arrays and objects in JavaScript. We will cover topics such as flattening arrays, merging objects, filtering and mapping arrays, and much more. By the end of this post, you will have a deeper understanding of JavaScript's array and object manipulation capabilities.