Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a map function on a 'Map' to change values

I can use a Map and then set values:

const a = new Map([["a", "b"], ["c", "d"]])

Now if I want to apply a function to all values in a functional way (without for ... of or .forEach), I thought I could have done something like this:

const b = a.map(([k, v]) => [k, doSomethingWith(v)]);

But there is no map function on a Map. Is there a built-in/elegant way to map a Map?

like image 458
rap-2-h Avatar asked Apr 24 '19 08:04

rap-2-h


People also ask

How do you change the value of an object on a map?

Use the set() method to update a value in a Map , e.g. map. set('key', 'value') . The set() method adds or updates the element with the provided key and value and returns the Map object.

How do you use the map function?

map() function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple etc.) Parameters : fun : It is a function to which map passes each element of given iterable. iter : It is a iterable which is to be mapped.

Does map function modify original array?

map() creates a new array from calling a function for every array element. map() calls a function once for each element in an array. map() does not execute the function for empty elements. map() does not change the original array.

How do you update an array on a map?

To update all the elements in an array: Call the map() method to iterate over the array. On each iteration, return the updated value. The map() method will return a new array that contains the updated values.

What is map function in Python?

Python map() is a built-in function that applies a function on all the items of an iterator given as input. An iterator, for example, can be a list, a tuple, a string, etc. and it returns an iterable map object.

What is map () function in JavaScript?

.map () accepts a callback function as one of its arguments, and an important parameter of that function is the current value of the item being processed by the function. This is a required parameter. With this parameter, you can modify each item in an array and create a new function.

What is the use of map values in Java?

Map Values () Method in Java With Examples Last Updated : 10 Sep, 2020 Map Values () method returns the Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa.

What is the use of map () method?

One of the most popular methods is the.map () method..map () creates an array from calling a specific function on each item in the parent array..map () is a non-mutating method that creates a new array as opposed to mutating methods, which only make changes to the calling array. This method can have many uses when working with arrays.


2 Answers

You could use Array.from for getting entries and mapping new values and take this result for a new Map.

const b = new Map(Array.from(
    a, 
    ([k, v]) => [k, doSomethingWith(v)]
));
like image 150
Nina Scholz Avatar answered Oct 24 '22 20:10

Nina Scholz


The most elegant/concise way I am aware of is by converting the map to an array with the spread operator (...), applying .map to that array and then creating a Map from it again:

const a = new Map([["a", "b"], ["c", "d"]])
const b = new Map([...a].map(([k,v])=>[k, v.toUpperCase()]))
// b: Map(2) {"a" => "B", "c" => "D"}
like image 41
nitzel Avatar answered Oct 24 '22 20:10

nitzel