Categories
JavaScript Answers

How to call .map() a JavaScript ES6 Map with JavaScript?

Spread the love

Sometimes, we want to call .map() a JavaScript ES6 Map with JavaScript.

In this article, we’ll look at how to call .map() a JavaScript ES6 Map with JavaScript.

How to call .map() a JavaScript ES6 Map with JavaScript?

To call .map() a JavaScript ES6 Map with JavaScript, we convert it to an array, and then back to a map.

For instance, we write

const myMap = new Map([
  ["thing1", 1],
  ["thing2", 2],
  ["thing3", 3],
]);
const newEntries = Array.from(myMap, ([key, value]) => [key, value + 1]);
const newMap = new Map(newEntries);

to call Array.from with myMap and a callback to map the values to a new values for the map.

Then we create the map from the newEntries array.

Conclusiin

To call .map() a JavaScript ES6 Map with JavaScript, we convert it to an array, and then back to a map.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *