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:
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