Skip to main content

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

When working with collections of numbers in Map or Set objects, a common task is to find the minimum or maximum value. The modern and most direct way to do this is by combining the Math.min() and Math.max() functions with the spread syntax (...).

This guide will teach you how to find the extreme values in both Maps and Sets. You will also learn the more advanced reduce() pattern for finding the entire key-value pair that corresponds to the min or max value in a Map.

The Core Method: Math.min/max with the Spread Syntax

The Math.min() and Math.max() functions are the standard tools for finding the smallest and largest numbers from a list of arguments. However, they do not accept an array or iterator directly.

To solve this, we use the spread syntax (...), which unpacks the elements of an iterable (like an array, Set, or a Map's values) and passes them as individual arguments to the function.

const numbers = [10, 20, 5];

// The spread syntax turns the array into a list of arguments.
const max = Math.max(...numbers); // equivalent to Math.max(10, 20, 5)

console.log(max); // Output: 20
note

This is the fundamental pattern we will use for both Sets and Maps.

Use Case 1: Finding the Min/Max Value in a Set

Since a Set is a simple collection of values and is directly iterable, this is a very straightforward task.

For example, we have a Set of numbers and need to find the smallest and largest values within it.

// Problem: How to find the min and max values in this Set?
const numbers = new Set([3, 8, 1, 10]);

Solution:

const numbers = new Set([3, 8, 1, 10]);

// Unpack the Set's values directly into the Math functions.
const minValue = Math.min(...numbers);
const maxValue = Math.max(...numbers);

console.log('Min:', minValue); // Output: Min: 1
console.log('Max:', maxValue); // Output: Max: 10

Use Case 2: Finding the Min/Max Value in a Map

A Map stores key-value pairs. To find the min or max of its values, we first need to get an iterator for just the values using the map.values() method and convert it to an array. Then we use max() and min() to find max and min values.

For example, we have a Map and we want to find the highest and lowest of its numeric values.

// Problem: Find the min and max of the values in this Map.
const scores = new Map([
['Alice', 85],
['Bob', 92],
['Charlie', 78],
]);

Solution:

const scores = new Map([
['Alice', 85],
['Bob', 92],
['Charlie', 78],
]);

// Convert the values iterator to an array
const scoreArray = [...scores.values()];

const minScore = Math.min(...scoreArray);
const maxScore = Math.max(...scoreArray);

console.log('Min Score:', minScore); // Output: Min Score: 78
console.log('Max Score:', maxScore); // Output: Max Score: 92

Use Case 3: Finding the Map Entry with the Min/Max Value

More often, you need not just the value, but the entire [key, value] pair or the object that contains the extreme value. For this, the reduce() method is the most efficient functional approach.

For example, we want to find the entire entry (['Bob', 92]) that corresponds to the highest score.

The solution is that we can convert the Map's entries to an array and then reduce them to find the entry with the highest value.

const scores = new Map([
['Alice', 85],
['Bob', 92],
['Charlie', 78],
]);

// Convert the Map entries to an array of [key, value] pairs
const entries = [...scores.entries()];

// Use reduce to find the entry with the maximum value
const maxEntry = entries.reduce((bestEntry, currentEntry) => {
// Compare the values (at index 1) of the entries
return currentEntry[1] > bestEntry[1] ? currentEntry : bestEntry;
});

console.log('Entry with max score:', maxEntry);
// Output: Entry with max score: ['Bob', 92]

// You can then access the key and value
const [winnerName, winnerScore] = maxEntry;
console.log(`${winnerName} has the highest score: ${winnerScore}`);
// Output: Bob has the highest score: 92

Conclusion

Finding the minimum or maximum value in a Set or Map is a simple task with modern JavaScript syntax.

  • The core method is to use Math.min() or Math.max() combined with the spread syntax (...).
  • For a Set, you can spread its values directly: Math.max(...mySet).
  • For a Map, you must first get an iterator for its values: Math.max(...myMap.values()).
  • If you need the entire Map entry (key and value), the most efficient functional approach is to use the reduce() method.