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'