Get Key with Highest Value from a JavaScript Object

September 3rd, 2021

Suppose you have the following JavaScript object and you need to find the key with the highest value - which is b in this case.

const numberObj = {
  a: 1,
  b: 50,
  c: 12

Here's a quick function to help you do that. The solution is written in TypeScript, so if you are looking for a pure JS solution, just remove the types 😊.

function getMaxValueKey(obj: {[key: string]: number}): string {
  return Object.keys(obj).reduce((a, b) => obj[a] > obj[b] ? a : b);

getMaxValueKey(numberObj) // 'b'
©2021 Michael Movsesov, All Rights Reserved