One data grid for JavaScript, React, Vue & Angular. Sorting, filtering, virtualization, pinning, and CSV export included. Early access $199 $349 → [Get it now]

How to filter an array in JavaScript

Filtering arrays based on specific conditions is fundamental for data processing, search functionality, and creating subsets of data that match user criteria in JavaScript applications. With over 25 years of experience in software development and as the creator of CoreUI, I’ve implemented array filtering extensively in components like search bars, data tables, and dashboard filters where users need to narrow down large datasets. From my extensive expertise, the most powerful and functional approach is using the filter() method, which creates a new array containing only elements that pass a test condition. This method is immutable, chainable, and provides excellent readability for complex filtering logic.

Use the filter() method to create a new array with elements that meet a specific condition.

const numbers = [1, 2, 3, 4, 5, 6]
const evenNumbers = numbers.filter(num => num % 2 === 0)
// Result: [2, 4, 6]

The filter() method creates a new array by testing each element against a provided function and including only those that return true. In this example, num => num % 2 === 0 tests if each number is even, and filter() creates a new array containing only the even numbers [2, 4, 6]. The original array remains unchanged. The callback function receives three arguments: the current element, its index, and the entire array, giving you full control over the filtering logic.

Best Practice Note:

This is the same approach we use in CoreUI components for implementing search functionality and data table filtering. The CSmartTable component uses filter() internally to power its built-in column filtering and search features.

For performance with large datasets, prefer a single filter with multiple conditions instead of chaining:

const results = items.filter(item => item.active && item.category === 'premium')

If you are working on similar problems, these guides are good follow-ups:


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to Conditionally Add a Property to an Object in JavaScript
How to Conditionally Add a Property to an Object in JavaScript

How to Add a Tab in HTML
How to Add a Tab in HTML

How to loop inside React JSX
How to loop inside React JSX

What's inside CoreUI Pro Next.js Boilerplate and who it is really for
What's inside CoreUI Pro Next.js Boilerplate and who it is really for

Answers by CoreUI Core Team