JavaScript - Print Object by id in an Array of Objects Last Updated : 09 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Here are the various methods to print objects by id in an array of objects in JavaScript1. Using Array.filter() MethodThe Array.filter() method, creates a new array containing only the object with the specified ID. JavaScript //Driver Code Starts const a = [ { id: 1, name: "Alia" }, { id: 2, name: "Dua" }, { id: 3, name: "Raha" }, { id: 4, name: "Charu" }, ]; //Driver Code Ends const id = 2; const result = a.filter(obj => obj.id === id); console.log(result[0]); Output{ id: 2, name: 'Dua' } In this exampleArray.filter() creates a new array containing objects that meet the condition (obj.id === id).Since filter returns an array, [0] is used to access the first matching object.2. Using for loop The for loop first we are iterating the array and searching in which object the given id is present and after that, we are printing the property we wanted. JavaScript let a = [{ id: 1, name: "a" },{ id: 2, name: "b" },{ id: 3, name: "c" }]; let id = 2; let prop = "name"; let res = a.find(item => item.id === id); if (res) console.log(res[prop]); Outputb In this exampleThe find() method is used to search for the first object in the array where item.id === id.Once found, we log the property specified by prop (e.g., "name").3. Using Underscore.js _.find() FunctionThe _.find() function looks at each element of the list and returns the first occurrence of the element that satisfy the condition. JavaScript const _ = require('underscore'); let a = [{ id: 1, name: "a" }, { id: 2, name: "b" }, { id: 3, name: "c" }]; let id = 2; let prop = "name"; let obj = _.find(a, { id: id }); console.log(obj?.[prop]); OutputbIn this example_.find(data, { id: id }) simplifies the search by passing an object with id: id instead of using a function.obj?.[prop] uses optional chaining to safely access the property, ensuring that it doesn't throw an error if obj is undefined.4. Using a Map Data StructureThe Map data structure to efficiently store objects by their id as keys. JavaScript let a = [{ id: 1, name: "a" }, { id: 2, name: "b" }, { id: 3, name: "c" },]; let map = new Map(); a.forEach(obj => map.set(obj.id, obj)); let reqId = 2; let reqObj = map.get(reqId); let reqProp = "name"; console.log(reqObj[reqProp]); Outputb In this exampleThe code stores objects in a Map using their id as the key. It then retrieves an object by its id, and dynamically accesses and prints a specified property (name) of that object using Obj[prop]. Comment More infoAdvertise with us Next Article JavaScript - Print Object by id in an Array of Objects devi_johns Follow Improve Article Tags : JavaScript Web Technologies javascript-array javascript-object JavaScript-Questions +1 More Similar Reads 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 How to get all the methods of an object using JavaScript? In this article, we will learn how to get all the methods of an object using JavaScript. In JavaScript, we can get all the methods of an object by iterating over each object and checking if its property value is a function. An HTML document contains some methods and the task is to get all methods of 2 min read Like