How to Implement filter() for Objects in JavaScript?
Last Updated :
28 Jun, 2025
To filter objects within an array in JavaScript, we can implement a custom filter() method. While JavaScript’s built-in filter() method works well for arrays, we may want to filter objects based on specific properties or combinations of properties.
1. Filtering an Object by Its Values
A common use case for filtering an object is to keep only those properties where the value meets a certain condition. For instance, we may have an object representing people and their ages, and we want to retain only the people who are older than 30.
JavaScript
const people = {
Jiya: 25,
Alia: 35,
Bobby: 45,
Sara: 28
};
const filtered = Object.fromEntries(
Object.entries(people).filter(([name, age]) => age > 30)
);
console.log(filtered);
Output{ Alia: 35, Bobby: 45 }
In this example
- Object.entries(people) converts the object into an array of key-value pairs: [["Jiya", 25], ["Alia", 35], ["Bobby", 45], ["Sara", 28]].
- .filter() applies a condition that checks if the value (age) is greater than 30.
- Object.fromEntries() converts the filtered array back into an object.
2. Filtering an Object by Its Keys
Filtering an object by its keys involves selecting or removing specific properties from an object based on the keys. This is useful when us want to exclude certain properties or focus only on a subset of keys.
JavaScript
const animal = {
cat: { color: 'white', age: 5 },
dog: { color: 'brown', age: 3 },
bird: { color: 'blue', age: 2 }
};
const filtered = Object.fromEntries(
Object.entries(animal).filter(([key, value]) => key !== 'dog')
);
console.log(filtered);
Output{ cat: { color: 'white', age: 5 }, bird: { color: 'blue', age: 2 } }
In this example
- Object.entries(animalData) converts the object to an array of key-value pairs.
- .filter() filters out the dog key.
- Object.fromEntries() creates a new object with the filtered data.
3. Filtering an Array of Objects
Filtering an array of objects is a common operation in JavaScript when working with collections of data. Often, we may need to filter an array of objects based on specific property values.
JavaScript
const people = [
{ name: 'Jiya', age: 25 },
{ name: 'Alia', age: 35 },
{ name: 'Bobby', age: 45 },
{ name: 'Sara', age: 28 }
];
const filtered = people.filter(person => person.age > 30);
console.log(filtered);
Output[ { name: 'Alia', age: 35 }, { name: 'Bobby', age: 45 } ]
In this example
- .filter() is applied to the array of objects, and the condition person.age > 30 filters out those who are older than 30.
4. Filtering with Multiple Conditions
When filtering an array of objects, you might need to apply multiple conditions simultaneously. This allows us to filter objects based on more than one property or multiple criteria.
JavaScript
const products = [
{ name: 'Laptop', price: 800, category: 'electronics' },
{ name: 'Shoes', price: 50, category: 'fashion' },
{ name: 'Watch', price: 150, category: 'electronics' },
{ name: 'Shirt', price: 20, category: 'fashion' }
];
const filtered = products.filter(product =>
product.price < 100 && product.category === 'fashion'
);
console.log(filtered);
Output[
{ name: 'Shoes', price: 50, category: 'fashion' },
{ name: 'Shirt', price: 20, category: 'fashion' }
]
In this example
- The .filter() method checks two conditions using the && (AND) operator.
- It keeps only products with a price less than 100 and belonging to the 'fashion' category.
Similar Reads
How to implement a filter() for Objects in JavaScript ? In this article, we will learn how to implement a filter() for Objects in JavaScript. In JavaScript, the filter() method is a built-in function for the array, not for the object. The filter() method outputs all the elements of an array that pass a specific test or satisfies a specific function. The
3 min read
How to filter nested objects in JavaScript? The filter() method creates a new array with all elements that pass the test implemented by the provided function. Below are the approaches used to filter nested objects in JavaScript: Table of Content Using filter() methodUsing some() method Method 1: Using filter() method Example: This approach us
2 min read
How to Compare Objects in JavaScript? Comparing objects is not as simple as comparing numbers or strings. Objects are compared based on their memory references, so even if two objects have the same properties and values, they are considered distinct if they are stored in different memory locations. Below are the various approaches to co
3 min read
How to Access Array of Objects in JavaScript ? Accessing an array of objects in JavaScript is a common task that involves retrieving and manipulating data stored within each object. This is essential when working with structured data, allowing developers to easily extract, update, or process information from multiple objects within an array.How
4 min read
How to compare Arrays of Objects in JavaScript? In JavaScript, comparing arrays of objects can be more complex than comparing primitive data types. We will discuss different ways to compare arrays of objects effectively, with detailed code examples and explanations.Syntax: Before going to detail the comparison techniques, let's first understand h
5 min read
How to Fix "filter is not a function" Error in JavaScript? JavaScript filter() method is a powerful tool for creating a new array with elements that pass a specified test from an existing array. However, we might encounter an error that says "filter is not a function". This error occurs when you attempt to use the filter method on an object that is not an a
3 min read