Skip to content

JavaScript Map values to Array | Convert

  • by

Use values() method and Array from() method to convert Map values to Array in JavaScript. The values() method get an iterator object that contains all of the values in the Map.

Array.from(map.values())

The Array.from method converts the iterable into an array and returns the new array instance.

JavaScript map values to array

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <script >
    var map = new Map();  
    map.set(1,"ABC");  
    map.set(2,"XYZ");  
    map.set([],"PQR");  

    const values = Array.from(map.values());
    console.log(values);
  </script>
</body>
</html>

Output:

JavaScript Map values to Array convert

You can convert Map Values to an Array using spread operator

<script >
    var map = new Map();  
    map.set(1,"ABC");  
    map.set(2,"XYZ");  
    map.set([],"PQR");  

    const values = [...map.values()];
    console.log(values);
</script>

Do comment if you have any doubts or suggestions on this JS map topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

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