You can use the Object.fromEntries()
method to transform the map into an object using array destructuring to separate the entries into keys and values. Here's an example:
index.tsx184 chars9 lines
In the example above, we create a new Map
instance and set two key-value pairs. Then we use Object.fromEntries()
method to transform the Map
into an object. Note that Map.entries()
returns an iterable object that yields an array of [key, value]
for each element in the map.
Array destructuring is used to separate out each element of this array into separate variables, which become the key and value pairs of the resulting object.
Note that you will need to ensure your JavaScript environment supports Object.fromEntries()
method. Alternatively, you can use polyfills for this method (available on MDN).
gistlibby LogSnag