How to create a map from array - JavaScript

Mo Ibra

Mo Ibra

Dec 19, 2022

In this article we will learn how to create a map from an array.

It is very common to create a map from an array in JavaScript.


Problem

We have a problem, which is that we want to create a map from the array

Imagine we have an array like this:

// Array
const array = [
    ['name', 'John'],
    ['age', 30],
];

And we want to create a map from this array.

Solution:

// Array
const array = [
    ['name', 'John'],
    ['age', 30],
];

// Convert to map
const map = new Map(array);

// Result:
console.log(map);

Output

Map(2) { 'name' => 'John', 'age' => 30 }

Thank you for reading

Thank you for reading my blog. 🚀

Suggestions For More Articles: