Hello, I have the following object:
{
h: 1,
o: 5,
l: 3,
a: 6,
m: 3,
i: 4,
n: 3,
b: 2,
r: 4,
e: 5,
s: 3,
y: 1,
u: 1,
v: 2,
j: 1
}
How do you return the letter that has the highest value? it's a test, thanks
Hello, I have the following object:
{
h: 1,
o: 5,
l: 3,
a: 6,
m: 3,
i: 4,
n: 3,
b: 2,
r: 4,
e: 5,
s: 3,
y: 1,
u: 1,
v: 2,
j: 1
}
How do you return the letter that has the highest value? it's a test, thanks
It would have to be in three steps.
values
.key/value
fromentries
objeto
Explanation
Math.max
It helps us get the maximum value from various. Since we are only interested in the values ofvalues
we convert all the values to an array withObject.values(obj)
and deconstruct them with...
.Then we iterate on
Object.entries
, which what it does is convert{a: 1, b: 2}
to[[a, 1], [b, 2]]
and we filter whenever the value is themaxValue
The third step
filterObj = Object.fromEntries(filterEntries);
converts us[[b, 2]]
once{ b: 2}
and filtered.If you want to specifically find the key and value of the maximum, you could do it like this:
You do not clarify what happens if the highest value is repeated, but this could be a solution: I convert the object into an array to be able to apply the function to it
sort
, I order from highest to lowest, I get the first element and with it I build the result.Another way you can do it is by using a
reduce
: