Skip to content

JavaScript map get all values | Example code

  • by

Use Array from() method with values() method to get the get all values from JavaScript map object. This solution will return a array with maps object element values.

The Array.from method creates a new array from an iterable object. and Map.values method to get an iterator object containing the values of the Map.

JavaScript map get all values

Simple example code.

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

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

Output:

JavaScript map get all values | Example code

Another alternative could be using Spread syntax (...).

const map = new Map().set(4, '4').set(2, '2');
console.log([...map.values()]);

Output: [ “4”, “2” ]

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

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading