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

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
4 min

banner

# Table of Contents

  1. Get the Min/Max Values in a Map in JavaScript
  2. Get the Min/Max Values in a Set in JavaScript

# 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

get min max values in map

The code for this article is available on GitHub

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
The code for this article is available on GitHub

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 ]

get min max values in map using reduce

The code for this article is available on GitHub

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

get min max values in set

The code for this article is available on GitHub

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 code for this article is available on GitHub

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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

Copyright ยฉ 2024 Borislav Hadzhiev