get the maximum value of map values in javascript

To get the maximum value of map values in JavaScript, you can convert the map to an array using the spread operator and then use the Math.max() method to find the maximum value. Here's an example:

index.tsx
const myMap = new Map([
  ['a', 50],
  ['b', 80],
  ['c', 30],
  ['d', 120],
]);

const mapValues = [...myMap.values()];
const maxValue = Math.max(...mapValues);

console.log(maxValue); // Output: 120
201 chars
12 lines

In this example, we first define a myMap with key-value pairs. We then use the spread operator to create a new array mapValues that contains all values from the map. Finally, we use the spread operator again with the Math.max() function to find the maximum value in the mapValues array.

Note that this method will work for any iterable object that contains numeric values.

gistlibby LogSnag