In ReactJS, you can loop over a map using Object.entries()
method to convert the Map
object into an array of key-value pairs, and then use the map()
method to loop over the entries and return an array of React elements. Here is an example:
const myMap = new Map();
myMap.set("key1", "value1");
myMap.set("key2", "value2");
const entriesArray = [...myMap.entries()];
const mappedValues = entriesArray.map(([key, value]) => {
return <p key={key}>{value}</p>;
});
return (
<div>
{mappedValues}
</div>
);