Retrieve key with largest value from a map in JavaScript
This post will discuss how to retrieve the key with the largest value from a map in JavaScript.
There are several ways to get a key with maximum value in a map in JavaScript. Here are some possible functions:
1. Using Array.reduce() function
If we have a Map object as a map, we can use the Map.entries() function to get an iterator of the key-value pairs, and then use the Array.reduce() function to compare the values and return the pair with the maximum value. The following code illustrates this:
|
1 2 3 4 5 |
let map = new Map([['a', 2], ['b', 4], ['c', 6]]); let pair = [...map.entries()].reduce((acc, entry) => entry[1] > acc[1] ? entry : acc); console.log(pair); // [c, 6] |
If we have an object literal as a map, we can use the Object.keys() function to get an array of keys, and then use the Array.reduce() function to compare the values and return the key with the maximum value. The following code illustrates this:
|
1 2 3 4 5 |
let obj = { a: 2, b: 4, c: 6 }; const maxKey = Object.keys(obj).reduce((a, b) => obj[a] > obj[b] ? a : b); console.log(maxKey); // c |
2. Using Array.filter() function
The Array.reduce() function will only return one pair, even if there are multiple pairs with the same maximum value. If we want to get an array of all the pairs with the maximum value, we can use the Array.filter() function instead. The following code illustrates its usage:
|
1 2 3 4 5 6 |
let map = new Map([['a', 2], ['b', 4], ['c', 6], ['d', 6]]); const max = Math.max(...map.values()); const maxPairs = [...map.entries()].filter(entry => entry[1] === max); console.log(maxPairs); // [[c, 6], [d, 6]] |
This function is simple and readable, but it requires an extra iteration. If we have an object literal as a map, we can do something like this:
|
1 2 3 4 5 6 |
let obj = { a: 2, b: 4, c: 6, d: 6 }; const max = Math.max(...Object.values(obj)); const maxKeys = Object.keys(obj).filter(key => obj[key] === max); console.log(maxKeys); // ['c', 'd'] |
3. Using a custom function
If we want a more generic and efficient way to get the min and max values in a map and the corresponding keys, we can use a custom function that takes a map as an argument and returns an object with four properties: minKey, minValue, maxKey, and maxValue. The function will iterate over the map’s entries only once and update these properties accordingly. The following code illustrates this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
function getMinMax(map) { let result = { minKey: null, minValue: Infinity, maxKey: null, maxValue: -Infinity }; for (let [key, value] of map) { if (value < result.minValue) { result.minKey = key; result.minValue = value; } if (value > result.maxValue) { result.maxKey = key; result.maxValue = value; } } return result; } let map = new Map([['a', 2], ['b', 4], ['c', 6], ['d', -1]]); // {minKey: "d", minValue: -1, maxKey: "c", maxValue: 6} console.log(getMinMax(map)); |
This function will only return one key for each min and max value. If we want to get all the keys with the same min or max value, we can modify the function to use arrays instead of single values for the minKey and maxKey properties. However, this will also require more time and space to create and update these arrays. The following code illustrates this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
function getMinMax(map) { let result = { minKeys: [], minValue: Infinity, maxKeys: [], maxValue: -Infinity }; for (let [key, value] of map) { if (value < result.minValue) { result.minKeys = [key]; result.minValue = value; } else if (value === result.minValue) { result.minKeys.push(key); } if (value > result.maxValue) { result.maxKeys = [key]; result.maxValue = value; } else if (value === result.maxValue) { result.maxKeys.push(key); } } return result; } let map = new Map([['a', 2], ['b', 4], ['c', 6], ['d', -1], ['e', -1], ['f', 6]]); // {minKeys: ["d", "e"], minValue: -1, maxKeys: ["c", "f"], maxValue: 6} console.log(getMinMax(map)); |
4. Using Lodash library
Finally, we can use a third-party library such as lodash for this task. Lodash has a function called _.maxBy(array, iteratee) that takes an array and an iteratee function as arguments and returns the element with the highest computed iteratee value. We can use this function to get the key with the maximum value from a map by passing it to the iteratee function. The following code illustrates its usage:
|
1 2 3 4 5 6 7 8 9 10 |
// import lodash module let _ = require("lodash"); // Create a map with some values let map = new Map([['a', 2], ['b', 4], ['c', 6]]); // Get the key with the maximum value using lodash let maxKey = _.maxBy(Array.from(map.keys()), key => map.get(key)); console.log(maxKey); // "c" |
This function will only return one key, even if there are multiple keys mapped with the maximum value. If we have an object literal as a map, we can do like:
|
1 2 3 4 5 6 7 8 9 10 |
// import lodash module let _ = require("lodash"); // Create an object with some key-value pairs let obj = { a: 2, b: 4, c: 6, d: 6 }; // Get the key with the maximum value using lodash let maxKey = _.maxBy(Object.keys(obj), key => obj[key]); console.log(maxKey); // "c" |
That’s all about retrieving the key with the largest value from a map in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)