How to implement a filter() for Objects in JavaScript ? Last Updated : 03 Sep, 2024 Comments Improve Suggest changes Like Article Like Report 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 return type of the filter() method is an array that consists of all the elements for which the function callback returns true. But for the object, we have to build a custom filter() method.Below are the methods for implementing filter() for Objects in JavaScript:Table of ContentUsing for loop and filter() MethodsUsing Object.entries() and Object.fromEntries() MethodUsing Array.prototype.reduce() for Filtering ObjectsApproach 1: Using for loop and filter() MethodsIn this approach, we are using the filter() method for filtering out the objects and using a loop for printing the multiple results.Example: The example returns IT department employees with the help of the filter() method. javascript let employees = [ { name: "Tony Stark", department: "IT" }, { name: "Peter Parker", department: "Pizza Delivery" }, { name: "Bruce Wayne", department: "IT" }, { name: "Clark Kent", department: "Editing" } ]; let output = employees.filter(employee => employee.department == "IT"); for (let i = 0; i < output.length; i++) { console.log(output[i].name) }; OutputTony Stark Bruce Wayne Approach 2: Using Object.entries() and Object.fromEntries() MethodIn this approach, we will create a custom built-in function for filtering the object.Example: This example shows the implementation of the above-explained approach. JavaScript let employees = [ { name: "Tony Stark", id: 1, department: "IT" }, { name: "Peter Parker", id: 2, department: "Pizza Delivery" }, { name: "Bruce Wayne", id: 3, department: "IT" }, { name: "Clark Kent", id: 4, department: "Editing" } ]; Object.filter = (obj, predicate) => Object.fromEntries(Object.entries(obj). filter(([key, value]) => predicate(value))); let filtered = Object.filter(employees, employee => employee.department === "IT"); console.log(filtered); Output{ '0': { name: 'Tony Stark', id: 1, department: 'IT' }, '2': { name: 'Bruce Wayne', id: 3, department: 'IT' } } Approach 3: Using Array.prototype.reduce() for Filtering ObjectsIn this approach, we use the reduce() method to create a custom filter function for objects. The reduce() method iterates over each entry in the object, applying a condition to determine whether to include the entry in the final filtered result.Example: Implementing a Custom filter() Function with reduce() JavaScript let employees = [ { name: "Nikunj", id: 1, department: "IT" }, { name: "Arti", id: 2, department: "Pizza Delivery" }, { name: "Dhruv", id: 3, department: "IT" }, { name: "Yash", id: 4, department: "Editing" } ]; const customFilter = (arr, predicate) => { return arr.reduce((acc, item) => { if (predicate(item)) { acc.push(item); } return acc; }, []); }; let filtered = customFilter(employees, employee => employee.department === "IT"); console.log(filtered); Output[ { name: 'Nikunj', id: 1, department: 'IT' }, { name: 'Dhruv', id: 3, department: 'IT' } ] Comment More infoAdvertise with us Next Article How to implement a filter() for Objects in JavaScript ? J Jasraj Follow Improve Article Tags : JavaScript Web Technologies javascript-object JavaScript-Questions Similar Reads How to get dynamic access to an object property in JavaScript ? In JavaScript, an object is a collection of properties, where each property consists of a key-value pair. Objects are a fundamental data type in JavaScript. You can create an object in JavaScript in the following ways: By using the object literal notation, which is a comma-separated list of key-valu 7 min read How to Remove an Entry by Key in JavaScript Object? In JavaScript, objects store data in the form of key-value pairs where the key may be any property of the object. In this article let us see how to remove key-value pairs a by given key in the object.Table of ContentUsing the delete operatorUsing the filter() methodUsing Destructuring and Object.ass 3 min read How to modify an object's property in an array of objects in JavaScript ? Modifying an object's property in an array of objects in JavaScript involves accessing the specific object within the array and updating its property.Using the Array.map() methodUsing the map() method to create a new array by transforming each element of the original array based on a specified funct 5 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 create object properties in JavaScript ? JavaScript is built on an object-oriented framework. An object is a collection of properties, where each property links a key to a value. These properties are not in any specific order. The value of a JavaScript property can be a method (function). Object properties can be updated, modified, added, 4 min read How to create an object with prototype in JavaScript ? In this article, we will discuss object creation & prototypes, along with understanding the different ways for object creation & their implementation through the examples. Prototypes are the mechanism by which objects in JavaScript inherit features from another object. A prototype property i 4 min read How to declare object with computed property name in JavaScript ? In this article, we learn how to declare an object with a computed property name. Before beginning this article, we have to know about the javascript objects. Computed Property Names: The ES6 computed property names feature allows us to compute an expression as a property name on an object. Javascri 2 min read How to add and remove properties from objects in JavaScript ? We will try to understand how to add properties to an object and how to add or remove properties from an object in JavaScript.Before we go and see the addition and removal of properties from an object let us first understand the basics of an object in JavaScript.Object:An object in JavaScript is a c 6 min read How to check if a value is object-like in JavaScript ? In JavaScript, objects are a collection of related data. It is also a container for name-value pairs. In JavaScript, we can check the type of value in many ways. Basically, we check if a value is object-like using typeof, instanceof, constructor, and Object.prototype.toString.call(k). All of the ope 4 min read JavaScript - Convert Two-Dimensional Array Into an Object Here are the different methods to convert the two-dimensional array into an object in JavaScript.1. Using a for LoopThe simplest way to convert a two-dimensional array into an object is by iterating through the array using a for loop and assigning key-value pairs.JavaScriptconst a = [['name', 'Abhay 2 min read Like