How to filter an array in array of objects in Javascript?

19    Asked by neeraj_7175 in Java , Asked on Jul 10, 2025

This question explains how to use the filter() method to extract objects from an array that meet certain criteria—such as matching values, ranges, or key presence.

Answered by RussellTEdelson

Filtering an array of objects in JavaScript is super common—whether you're working with product lists, user profiles, or API responses. Thankfully, JavaScript makes this really easy with the built-in .filter() method.

 Basic Syntax

  const filteredArray = array.filter(item => condition);

  • .filter() loops through each object in the array.
  • It returns a new array with only the objects that pass the condition (returns true).

 Example: Filter Users by Age

const users = [
  { name: "Alice", age: 25 },
  { name: "Bob", age: 17 },
  { name: "Charlie", age: 30 }
];
const adults = users.filter(user => user.age >= 18);
console.log(adults);
// Output: [{ name: "Alice", age: 25 }, { name: "Charlie", age: 30 }]

 Why Use .filter()?

It's clean, readable, and doesn’t mutate the original array.

Useful for:

  • Filtering search results
  • Cleaning up invalid data
  • Applying business rules (e.g., only active users)

In short, .filter() is your go-to tool for narrowing down an array of objects based on custom logic. It keeps your code concise and expressive—perfect for modern JavaScript development!



Your Answer

Interviews

Parent Categories