How to Use the JavaScript Filter Array Method

Avatar

By squashlabs, Last Updated: Oct. 6, 2023

How to Use the JavaScript Filter Array Method

Introduction to Filter Array Method

The JavaScript filter() method is a powerful tool that allows you to create a new array by filtering out elements from an existing array based on a specified condition. It provides a concise and elegant way to manipulate arrays without modifying the original array.

Here's the basic syntax of the filter() method:

let newArray = array.filter(callback(element[, index[, array]])[, thisArg])

The filter() method takes in a callback function as its argument, which is executed on each element of the array. The callback function should return true or false based on whether the element should be included in the new filtered array.

Let's dive into some practical examples to better understand how to use the filter() method.

Related Article: How to Use Ngif Else in AngularJS

Syntax of Filter Array Method

The syntax of the filter() method is simple and straightforward. It takes in two mandatory arguments: the callback function and an optional thisArg parameter. The callback function receives three optional parameters: element, index, and array. Here's a breakdown of each parameter:

- element (required): The current element being processed in the array.

- index (optional): The index of the current element being processed.

- array (optional): The array on which the filter() method was called.

Let's see an example of how to use the filter() method with the basic syntax:

const numbers = [1, 2, 3, 4, 5];

const evenNumbers = numbers.filter((number) => {
  return number % 2 === 0;
});

console.log(evenNumbers); // Output: [2, 4]

In the above example, we have an array of numbers. We use the filter() method to create a new array called evenNumbers that contains only the even numbers from the original array.

Use Case: Removing Falsy Values

One common use case of the filter() method is to remove falsy values (such as null, undefined, 0, false, and '') from an array. Let's take a look at an example:

const mixedArray = ['apple', null, 0, 'orange', undefined, false, '', 'banana'];

const truthyValues = mixedArray.filter((value) => {
  return value;
});

console.log(truthyValues); // Output: ['apple', 'orange', 'banana']

In the above example, we have an array called mixedArray that contains a mixture of falsy and truthy values. We use the filter() method to create a new array called truthyValues that only includes the truthy values.

Use Case: Filtering Out Specific Values

Another use case of the filter() method is to filter out specific values from an array based on a condition. Let's consider an example where we want to filter out all the negative numbers from an array:

const numbers = [1, -2, 3, -4, 5];

const positiveNumbers = numbers.filter((number) => {
  return number >= 0;
});

console.log(positiveNumbers); // Output: [1, 3, 5]

In the above example, we use the filter() method to create a new array called positiveNumbers that contains only the positive numbers from the original array.

Related Article: Exploring Client-Side Functionality of Next.js in JavaScript

Use Case: Retrieving Matching Elements

The filter() method can also be used to retrieve elements that match a specific condition. Let's say we have an array of objects representing books, and we want to filter out all the books that have a price greater than $50:

const books = [
  { title: 'Book 1', price: 30 },
  { title: 'Book 2', price: 60 },
  { title: 'Book 3', price: 20 },
  { title: 'Book 4', price: 80 },
];

const expensiveBooks = books.filter((book) => {
  return book.price > 50;
});

console.log(expensiveBooks);

In the above example, we use the filter() method to create a new array called expensiveBooks that contains only the books with a price greater than $50.

Best Practice: Validate Array before Filtering

Before applying the filter() method to an array, it's a good practice to validate whether the input is an actual array. This can help prevent unexpected errors or undesired behavior. Here's an example of how to validate an array before filtering:

function filterArray(array) {
  if (!Array.isArray(array)) {
    throw new Error('Invalid input: array is required.');
  }

  return array.filter((element) => {
    // Filter logic here
  });
}

In the above example, we define a reusable function called filterArray that checks if the input is an array using the Array.isArray() method. If the input is not an array, an error is thrown.

Best Practice: Avoiding Excessive Callbacks

When using the filter() method, avoid writing complex callback functions directly inside the filter() method. Instead, define the callback function separately and assign it a meaningful name. This improves code readability and maintainability. Here's an example:

const numbers = [1, 2, 3, 4, 5];

function isEven(number) {
  return number % 2 === 0;
}

const evenNumbers = numbers.filter(isEven);

console.log(evenNumbers); // Output: [2, 4]

In the above example, we define a separate function called isEven that checks if a number is even. This function is then passed as the callback function to the filter() method.

Real World Example: Filtering User Input

The filter() method can be used to filter and sanitize user input in real-world scenarios, such as form validation. Let's consider an example where we want to filter out any profanity from a user's input:

const userInput = 'Hello, this is a bad word!';

const sanitizedInput = userInput.split(' ').filter((word) => {
  const badWords = ['bad', 'profanity'];

  return !badWords.includes(word);
});

console.log(sanitizedInput.join(' ')); // Output: 'Hello, this is a word!'

In the above example, we split the user's input into an array of words using the split() method. We then use the filter() method to create a new array called sanitizedInput that excludes any words present in the badWords array.

Related Article: Implementing TypeORM with GraphQL and NestJS

Real World Example: Cleaning Data Sets

Another practical use case of the filter() method is cleaning data sets by removing unwanted elements. Let's consider an example where we have an array of transactions and we want to filter out any transactions with a negative amount:

const transactions = [
  { id: 1, amount: 100 },
  { id: 2, amount: -50 },
  { id: 3, amount: 200 },
  { id: 4, amount: -75 },
];

const positiveTransactions = transactions.filter((transaction) => {
  return transaction.amount >= 0;
});

console.log(positiveTransactions);

In the above example, we use the filter() method to create a new array called positiveTransactions that includes only the transactions with a non-negative amount.

Performance Consideration: Filter vs For Loop

When it comes to performance, the filter() method is generally slower than using a simple for loop, especially when dealing with large arrays. While the filter() method provides a more concise and readable syntax, it incurs the overhead of creating a new array.

Here's an example that demonstrates the performance difference between filter() and a for loop:

const numbers = Array.from({ length: 100000 }, (_, index) => index + 1);

// Using filter()
const evenNumbersFilter = numbers.filter((number) => {
  return number % 2 === 0;
});

// Using for loop
const evenNumbersForLoop = [];
for (let i = 0; i < numbers.length; i++) {
  if (numbers[i] % 2 === 0) {
    evenNumbersForLoop.push(numbers[i]);
  }
}

In the above example, we have an array of 100,000 numbers. We use both the filter() method and a for loop to filter out the even numbers. The for loop approach tends to be faster than the filter() method for large arrays.

Performance Consideration: Filter vs Reduce

In some cases, using the reduce() method can be more performant than using the filter() method followed by a map() or forEach() method. The reduce() method allows you to filter and transform an array in a single iteration.

Here's an example that demonstrates the performance improvement using reduce():

const numbers = Array.from({ length: 100000 }, (_, index) => index + 1);

// Using filter() followed by map()
const evenNumbersFilterMap = numbers
  .filter((number) => number % 2 === 0)
  .map((number) => number * 2);

// Using reduce()
const evenNumbersReduce = numbers.reduce((accumulator, number) => {
  if (number % 2 === 0) {
    accumulator.push(number * 2);
  }
  return accumulator;
}, []);

In the above example, we have an array of 100,000 numbers. We use both the filter() method followed by the map() method, and the reduce() method to filter out the even numbers and double their values. The reduce() method combines the filtering and transformation steps in a single iteration, potentially improving performance.

Performance Consideration: Preallocation of Result Array

When using the filter() method, it's a good practice to preallocate the result array to the expected size if possible. This can improve performance by avoiding unnecessary array resizing operations.

Here's an example that demonstrates the performance improvement by preallocating the result array:

const numbers = Array.from({ length: 100000 }, (_, index) => index + 1);

// Without preallocation
const evenNumbersWithoutPreallocation = numbers.filter((number) => {
  return number % 2 === 0;
});

// With preallocation
const evenNumbersWithPreallocation = new Array(numbers.length);

let index = 0;
numbers.forEach((number) => {
  if (number % 2 === 0) {
    evenNumbersWithPreallocation[index++] = number;
  }
});
evenNumbersWithPreallocation.length = index;

In the above example, we have an array of 100,000 numbers. We use both the filter() method without preallocation and a forEach() loop with preallocation to filter out the even numbers. The forEach() loop with preallocation is generally faster because it avoids the resizing of the result array.

Related Article: Sharing State Between Two Components in React JavaScript

Advanced Technique: Chaining Array Methods

The filter() method can be easily combined with other array methods, such as map(), reduce(), or forEach(), to perform complex operations on arrays. This technique is known as method chaining.

Here's an example that demonstrates method chaining with the filter() method:

const numbers = [1, 2, 3, 4, 5];

const doubledEvenNumbers = numbers
  .filter((number) => number % 2 === 0)
  .map((number) => number * 2);

console.log(doubledEvenNumbers); // Output: [4, 8]

In the above example, we filter out the even numbers using the filter() method and then double each even number using the map() method. The result is an array of doubled even numbers.

Advanced Technique: Using Filter with Other ES6 Features

The filter() method can be combined with other ES6 features, such as arrow functions, destructuring, and the spread operator, to write more concise and expressive code.

Here's an example that demonstrates the use of these features with the filter() method:

const books = [
  { title: 'Book 1', price: 30 },
  { title: 'Book 2', price: 60 },
  { title: 'Book 3', price: 20 },
  { title: 'Book 4', price: 80 },
];

const cheapBooks = books.filter(({ price }) => price < 50);

console.log(cheapBooks); // Output: [{ title: 'Book 1', price: 30 }, { title: 'Book 3', price: 20 }]

In the above example, we use destructuring to extract the price property from each book object within the filter() method. We also use an arrow function to write a concise condition. The result is an array of books with prices less than $50.

Code Snippet Idea: Removing Duplicate Elements

The filter() method can be used to remove duplicate elements from an array by leveraging the indexOf() method. Here's a code snippet idea that demonstrates this:

const array = [1, 2, 2, 3, 4, 4, 5];

const uniqueArray = array.filter((element, index, self) => {
  return self.indexOf(element) === index;
});

console.log(uniqueArray); // Output: [1, 2, 3, 4, 5]

In the above code snippet, we use the filter() method to create a new array called uniqueArray that only contains the elements that have a unique index within the original array.

Code Snippet Idea: Filtering Out Outliers in Data

The filter() method can be used to filter out outliers from a data set based on a specific condition. Here's a code snippet idea that demonstrates this:

const data = [10, 15, 20, 25, 30, 500, 35, 40, 45];

const filteredData = data.filter((value) => {
  const mean = data.reduce((sum, current) => sum + current, 0) / data.length;
  const standardDeviation = Math.sqrt(
    data.reduce((sum, current) => sum + (current - mean) ** 2, 0) / data.length
  );

  return Math.abs(value - mean) <= 2 * standardDeviation;
});

console.log(filteredData); // Output: [10, 15, 20, 25, 30, 35, 40, 45]

In the above code snippet, we use the filter() method to create a new array called filteredData that only includes the values within two standard deviations from the mean of the data set. This helps filter out any outliers that fall outside the normal range.

Related Article: How to Check for Null Values in Javascript

Code Snippet Idea: Using Filter in a Promise Chain

The filter() method can be used in a promise chain to filter out elements asynchronously based on a condition. Here's a code snippet idea that demonstrates this:

function getProducts(category) {
  return new Promise((resolve) => {
    setTimeout(() => {
      // Simulating an asynchronous API call
      resolve([
        { id: 1, name: 'Product 1', category: 'A' },
        { id: 2, name: 'Product 2', category: 'B' },
        { id: 3, name: 'Product 3', category: 'A' },
        { id: 4, name: 'Product 4', category: 'C' },
      ]);
    }, 1000);
  });
}

function filterProductsByCategory(category) {
  return getProducts().then((products) => {
    return products.filter((product) => product.category === category);
  });
}

filterProductsByCategory('A').then((filteredProducts) => {
  console.log(filteredProducts);
});

In the above code snippet, we have two functions: getProducts() and filterProductsByCategory(). The getProducts() function simulates an asynchronous API call that returns an array of products. The filterProductsByCategory() function filters out the products based on a specified category using the filter() method. Finally, we log the filtered products to the console.

Code Snippet Idea: Filtering Complex Data Structures

The filter() method can be used to filter complex data structures, such as arrays of objects or nested arrays. Here's a code snippet idea that demonstrates this:

const data = [
  { id: 1, name: 'John', age: 25 },
  { id: 2, name: 'Jane', age: 30 },
  { id: 3, name: 'Bob', age: 35 },
  { id: 4, name: 'Alice', age: 40 },
];

const filteredData = data.filter((person) => {
  return person.age >= 30;
});

console.log(filteredData);

In the above code snippet, we have an array of objects representing people. We use the filter() method to create a new array called filteredData that only includes the people with an age of 30 or above.

Code Snippet Idea: Implementing Custom Filter Functions

The filter() method allows you to implement custom filter functions to perform complex filtering operations. Here's a code snippet idea that demonstrates this:

Array.prototype.customFilter = function (callback) {
  const filteredArray = [];
  for (let i = 0; i  {
  return number % 2 === 0;
});

console.log(evenNumbers); // Output: [2, 4]

In the above code snippet, we define a custom customFilter() method on the Array.prototype object. This method works similarly to the native filter() method but allows for custom filtering logic. We then use this custom method to filter out the even numbers from an array.

Error Handling: Dealing with Non-Array Inputs

When using the filter() method, it's important to handle non-array inputs gracefully to prevent unexpected errors. Here's an example of how to handle non-array inputs:

function filterArray(array) {
  if (!Array.isArray(array)) {
    throw new TypeError('Invalid input: array expected.');
  }

  return array.filter((element) => {
    // Filter logic here
  });
}

try {
  const result = filterArray('not an array');
  console.log(result);
} catch (error) {
  console.error(error);
}

In the above example, we define a function called filterArray() that checks if the input is an array using the Array.isArray() method. If the input is not an array, a TypeError is thrown.

Related Article: How to Create a Navbar in Next.js

Error Handling: Handling Exceptions in Callback Functions

When writing callback functions for the filter() method, it's important to handle any potential exceptions or errors gracefully. This ensures that the filter() method does not terminate prematurely and allows for proper error handling.

Here's an example of how to handle exceptions in callback functions:

const numbers = [1, 2, 3, 4, 5];

try {
  const filteredNumbers = numbers.filter((number) => {
    if (number === 3) {
      throw new Error('Invalid number: 3');
    }
    return number % 2 === 0;
  });

  console.log(filteredNumbers);
} catch (error) {
  console.error(error);
}

In the above example, we have an array of numbers. Inside the callback function of the filter() method, we check if a number is equal to 3 and throw an error if it is. We use a try...catch block to handle any exceptions that may occur during the filtering process.

How To Disable & Enable a Submit Button With jQuery

Learn how to easily enable or disable the submit button using jQuery in JavaScript. This article provides two methods - using the prop() method and u… read more

Integrating HTMX with Javascript Frameworks

Integrating HTMX with JavaScript frameworks is a valuable skill for frontend developers. This article provides best practices for using HTMX with pop… read more

How to Use Javascript for Page Refresh: Reload Page jQuery

Using JavaScript to refresh or reload a page with jQuery can be a useful technique for web developers. This article focuses on two approaches: using … read more

How To Check If a Key Exists In a Javascript Object

Checking if a key exists in a JavaScript object is a common task for many software engineers. This article provides techniques to accomplish this. Yo… read more

How To Check For An Empty String In Javascript

Checking for an empty string in JavaScript is a common task for developers. This article explores simple techniques to determine if a string is undef… read more

How to Implement Functional Programming in JavaScript: A Practical Guide

Functional programming is a powerful paradigm that can greatly enhance your JavaScript code. In this practical guide, you will learn how to implement… read more

How to Create a Countdown Timer with Javascript

Creating a countdown timer using Javascript is a simple process that can enhance the functionality of your web applications. This article provides a … read more

How to Set Select Option Selected By Value in JQuery

Setting select options as selected by value in JQuery using JavaScript can be achieved through different methods. One approach is using the attribute… read more

Executing JavaScript Post UI Component Rendering

In this article, you will learn how to execute your JavaScript codes after the UI components have loaded. From understanding UI components and event … read more

How to Check If a Value is an Object in JavaScript

This article provides a simple guide on checking if a value is an object using JavaScript. It covers using the typeof operator, the instanceof operat… read more