I have a dictionary that looks like this
let highest_prob_list = {
"hi": 100,
"bye": 1,
};
I need to find the key with the greatest value in the dictionary.
For example, I would like "hi" from the object above.
How can i achieve this?
Use Object.keys to get the properties of the object, then reduce over the array to get the property with the highest value.
let obj = {
"hi": 100,
"bye": 1,
};
let props = Object.keys(obj)
const res = props.reduce((a,b) => a = obj[b] > obj[a] ? b : a, props[0])
console.log(res)
const findMax = () => {
const highest_prob_list = {
"hi": 100,
"bye": 1,
}
const keys = Object.keys(highest_prob_list)
let max = keys[0]
keys.forEach(key => {
if (highest_prob_list[key] > highest_prob_list[max]) {
max = key;
}
})
return max;
}