How to Convert an Array of Objects to Map in JavaScript?
Last Updated :
23 Jan, 2025
Here are the different methods to convert an array of objects into a Map in JavaScript
1. Using new Map() Constructor
The Map constructor can directly create a Map from an array of key-value pairs. If each object in the array has a specific key-value structure, you can map it accordingly.
JavaScript
const a = [
{ id: 1, value: 100 },
{ id: 2, value: 200 },
{ id: 3, value: 300 },
];
const m = new Map(a.map((o) => [o.id, o.value]));
console.log(m);
OutputMap(3) { 1 => 100, 2 => 200, 3 => 300 }
This approach is concise and works well for simple transformations.
2. Using reduce()
The reduce() method allows you to iteratively build a Map by accumulating key-value pairs.
JavaScript
const a = [
{ id: 10, score: 50 },
{ id: 20, score: 60 },
{ id: 30, score: 70 },
];
const m = a.reduce((acc, o) => {
acc.set(o.id, o.score);
return acc;
}, new Map());
console.log(m);
OutputMap(3) { 10 => 50, 20 => 60, 30 => 70 }
This method is highly flexible, making it ideal for more complex transformations.
3. Using forEach()
The forEach() method can iterate over the array and manually populate the Map.
JavaScript
const a = [
{ key: "A", val: 1 },
{ key: "B", val: 2 },
{ key: "C", val: 3 },
];
const m = new Map();
a.forEach((o) => m.set(o.key, o.val));
console.log(m);
OutputMap(3) { 'A' => 1, 'B' => 2, 'C' => 3 }
This method is simple and easy to understand for small arrays.
4. Using for-of Loop
The for-of loop is a clear and flexible way to iterate over the array and create a Map.
JavaScript
const a = [
{ id: 101, qty: 5 },
{ id: 102, qty: 10 },
{ id: 103, qty: 15 },
];
const m = new Map();
for (const o of a) {
m.set(o.id, o.qty);
}
console.log(m);
OutputMap(3) { 101 => 5, 102 => 10, 103 => 15 }
This approach provides control over the iteration logic.
5. Using Object.entries()
If the array of objects has a key-value structure, Object.entries() can be used to generate a Map.
JavaScript
const a = [
{ key: "p", value: 20 },
{ key: "q", value: 30 },
{ key: "r", value: 40 },
];
const m = new Map(a.map(({ key, value }) =>
[key, value]));
console.log(m);
OutputMap(3) { 'p' => 20, 'q' => 30, 'r' => 40 }
This is a concise and efficient way to handle key-value objects.
6. Handling Duplicates Using Set and Array.from()
If the array has duplicate keys, you can remove them using Set and Array.from() before creating a Map.
JavaScript
const a = [
{ id: 1, val: 50 },
{ id: 2, val: 60 },
{ id: 1, val: 50 },
];
const unique = Array.from(new Set(a.map((o) =>
o.id))).map((id) =>
a.find((o) => o.id === id)
);
const m = new Map(unique.map((o) => [o.id, o.val]));
console.log(m);
OutputMap(2) { 1 => 50, 2 => 60 }
This is effective for handling duplicates and creating a Map with unique keys.
7. Using Custom Logic in reduce()
For more advanced cases, you can apply additional logic inside reduce() to process the array and populate the Map.
JavaScript
const a = [
{ id: "X", count: 4 },
{ id: "Y", count: 8 },
{ id: "Z", count: 12 },
];
const m = a.reduce((acc, o) => {
if (o.count > 5) acc.set(o.id, o.count);
return acc;
}, new Map());
console.log(m);
OutputMap(2) { 'Y' => 8, 'Z' => 12 }
This approach allows for custom filtering or transformation during the conversion.
Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
What is an API (Application Programming Interface) In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of
10 min read
Introduction of Firewall in Computer Network A firewall is a network security device either hardware or software-based which monitors all incoming and outgoing traffic and based on a defined set of security rules it accepts, rejects, or drops that specific traffic. It acts like a security guard that helps keep your digital world safe from unwa
10 min read