Skip to content
the poor coder

js map array to dictionary

Poor Man
Published
Reading time
2 min read
On this page

Converting a JavaScript Array to a Dictionary using the Map method#

Have you ever needed to convert a JavaScript array into a dictionary? Well, you've come to the right place! In this post, we'll discuss how to use the map method to convert an array to a dictionary in JavaScript.

The Problem#

Imagine you have an array of objects that represent users:

Text

const users = [
  {id: 1, name: 'John'},
  {id: 2, name: 'Jane'},
  {id: 3, name: 'James'}
]

But now you need to access data from this array based on an id value. It would be much more efficient to convert this array into a dictionary where the keys are the ids and the values are the corresponding objects:

Text

const usersDict = {
  1: {id: 1, name: 'John'},
  2: {id: 2, name: 'Jane'},
  3: {id: 3, name: 'James'}
}

The Solution#

The map method can be used to convert an array into a dictionary. Here's how:

Text

const usersDict = users.map(user => ({[user.id]: user})).reduce((acc, obj) => Object.assign(acc, obj), {})

Let's break down what's happening here. First, we're using the map method to transform each object in the users array into a new object that has the id as the key and the entire object as the value:

Text

users.map(user => ({[user.id]: user}))

This will return an array of objects that look like this:

Text

[
  {1: {id: 1, name: 'John'}},
  {2: {id: 2, name: 'Jane'}},
  {3: {id: 3, name: 'James'}}
]

Next, we're using the reduce method to combine all of these objects into one dictionary:

Text

.reduce((acc, obj) => Object.assign(acc, obj), {})

This will merge each object into the accumulator object, which is initially an empty object. The end result is a single object that represents the dictionary we want:

Text

{
  1: {id: 1, name: 'John'},
  2: {id: 2, name: 'Jane'},
  3: {id: 3, name: 'James'}
}

Alternative Solution#

Another way to accomplish the same thing is to use the reduce method twice:

Text

const usersDict = users.reduce((acc, user) => ({...acc, [user.id]: user}), {})

This accomplishes the same thing as the previous solution, but uses the spread operator to merge each object into the accumulator rather than Object.assign.

Conclusion#

So there you have it! Two different ways to convert a JavaScript array into a dictionary using the map method. Hopefully this helps you next time you encounter this problem!

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC