# Get the Min/Max Values in a Map or a Set in JavaScript

Last updated: Mar 4, 2024
4 min

## #Get the Min/Max Values in a Map in JavaScript

To get the min and max values in a Map:

1. Use the values() method to get an iterator of the Map's values.
2. Pass the iterator to the Math.min() and Math.max() methods.
3. The Math.min() and Math.max() methods return the lowest and highest of the passed-in numbers.
index.js
const map1 = new Map([ ['num1', 3], ['num2', 5], ['num3', 8], ]); const min = Math.min(...map1.values()); console.log(min); // ๐๏ธ 3 const max = Math.max(...map1.values()); console.log(max); // ๐๏ธ 8

The Map.values() method returns an iterator object containing the Map's values.

index.js
const map1 = new Map([ ['num1', 3], ['num2', 5], ['num3', 8], ]); // ๐๏ธ [Map Iterator] { 3, 5, 8 } console.log(map1.values());

We can't pass the iterator object directly to the Math.max() and Math.min() methods because they expect multiple, comma-separated numbers and not an iterator.

index.js
const min = Math.min(3, 5, 8); console.log(min); // ๐๏ธ 3 const max = Math.max(3, 5, 8); console.log(max); // ๐๏ธ 8

We used the spread syntax (...) to unpack the values of the iterator objects in the call to the Math.min() and Math.max() methods.

index.js
const map1 = new Map([ ['num1', 3], ['num2', 5], ['num3', 8], ]); const min = Math.min(...map1.values()); console.log(min); // ๐๏ธ 3 const max = Math.max(...map1.values()); console.log(max); // ๐๏ธ 8

You can imagine that the spread syntax passes the values of the iterator object as comma-separated arguments to the methods.

The Math.min() method returns the smallest of the supplied parameters.

The Math.max() method returns the largest of the given numbers.

If you need to get the min and max values in a Map and the corresponding keys, use the Array.reduce() method.

## #Get the Min/Max Values in a Map using reduce()

This is a three-step process:

1. Use the reduce() method to iterate over the Map's entries.
2. Check if each value is the min or max value.
3. Return the corresponding key-value pair.
index.js
const map1 = new Map([ ['num1', 3], ['num2', 5], ['num3', 8], ]); const max = [...map1.entries()].reduce( (accumulator, element) => { return element[1] > accumulator[1] ? element : accumulator; }, ); console.log(max); // ๐๏ธ [ 'num3', 8 ] const min = [...map1.entries()].reduce( (accumulator, element) => { return element[1] < accumulator[1] ? element : accumulator; }, ); console.log(min); // ๐๏ธ [ 'num1', 3 ]

The Map.entries() method returns an iterator object containing the [key, value] pairs for each element in the Map.

index.js
const map1 = new Map([ ['num1', 3], ['num2', 5], ['num3', 8], ]); // ๐๏ธ [Map Entries] { [ 'num1', 3 ], [ 'num2', 5 ], [ 'num3', 8 ] } console.log(map1.entries());

The function we passed to the Array.reduce() method gets called for each element in the array.

On each iteration, we check if the current value is greater than (max) or less than (min) the accumulator and return the element or the accumulator.

Whatever we return from the callback function gets passed as the accumulator on the next iteration.

The code sample returns arrays containing the key and value of the min and max elements.

## #Get the Min/Max Values in a Set in JavaScript

To get the min and max values in a Set:

1. Use the spread syntax to unpack the Set in calls to the Math.min and Math.max methods.
2. The Math.min method will return the min value in the Set.
3. The Math.max method will return the max value in the Set.
index.js
const set1 = new Set([3, 5, 8]); const min = Math.min(...set1); console.log(min); // ๐๏ธ 3 const max = Math.max(...set1); console.log(max); // ๐๏ธ 8

Set objects are iterators, just like arrays.

We can't pass the iterator object directly to the Math.max() and Math.min() methods because they expect multiple, comma-separated numbers and not an iterator.

index.js
const min = Math.min(1, 3, 5); console.log(min); // ๐๏ธ 1 const max = Math.max(1, 3, 5); console.log(max); // ๐๏ธ 5

The Math.min() method returns the smallest of the supplied parameters.

The Math.max() method returns the largest of the given numbers.

We used the spread syntax (...) to unpack the values of the Set in the calls to the Math.min() and Math.max() methods.

index.js
const set1 = new Set([3, 5, 8]); const min = Math.min(...set1); console.log(min); // ๐๏ธ 3 const max = Math.max(...set1); console.log(max); // ๐๏ธ 8

You can imagine that the spread syntax passes the values of the Set as multiple, comma-separated arguments to the methods.

If you have to do this often, define a reusable function.

index.js
function setMinMax(set) { const min = Math.min(...set); const max = Math.max(...set); return {min, max}; } const set1 = new Set([3, 5, 8]); const result = setMinMax(set1); console.log(result); // ๐๏ธ { min: 3, max: 8 } console.log(result.min); // ๐๏ธ 3 console.log(result.max); // ๐๏ธ 8

The function takes a Set as a parameter and returns an object with min and max properties.

The min property stores the min value in the Set and the max property stores the max value.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
You can use the search field on my Home Page to filter through all of my articles.