Convert an array of objects to a Map in JavaScript

To convert an array of objects to a map, you can use the Array.map() method to iterate through the array elements to create an array of key-value pairs. Then pass the array of key-value pairs to the Map() constructor to create a Map object.

const users = [
  { name: 'John Doe', role: 'Admin' },
  { name: 'Alex Hales', role: 'Manager' },
  { name: 'Ali Feroz', role: 'User' }
]

const kvPairs = users.map(u => [u.name, u.role])

const map = new Map(kvPairs)
console.log(map)

// Map(3) {
//     'John Doe' => 'Admin',
//     'Alex Hales' => 'Manager',
//     'Ali Feroz' => 'User'
// }

We passed a callback function to the Array.map() method that gets called for each object in the array.

On each iteration, we return an array containing the key and the value for each object resulting in an array of key-value pairs:

const users = [
  { name: 'John Doe', role: 'Admin' },
  { name: 'Alex Hales', role: 'Manager' },
  { name: 'Ali Feroz', role: 'User' }
]

const kvPairs = users.map(u => [u.name, u.role])
console.log(kvPairs)

// [
//     [ 'John Doe', 'Admin' ],
//     [ 'Alex Hales', 'Manager' ],
//     [ 'Ali Feroz', 'User' ]
// ]

Finally, we pass the result to the Map() constructor to create a new instance of the Map object.

An alternate approach is to use the Array.forEach() method to convert the array of objects to a map. It is a three steps process:

  1. First, we use the Map() constructor to create an empty map object.
  2. Call the forEach() method on the array to iterate through all objects.
  3. On each iteration, use the Map.set() method to add the key value to the map.
const users = [
  { name: 'John Doe', role: 'Admin' },
  { name: 'Alex Hales', role: 'Manager' },
  { name: 'Ali Feroz', role: 'User' }
]

const map = new Map()

users.forEach(u => {
  map.set(u.name, u.role)
})

console.log(map)
// Map(3) {
//     'John Doe' => 'Admin',
//     'Alex Hales' => 'Manager',
//     'Ali Feroz' => 'User'
// }

Read this article to learn more about the Map object and how to use it to create collections of key-value pairs in JavaScript.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.