I want to reach the higher numbers in an object.
let object1 = {
a: 2,
b: 2,
c: 0,
d: 3,
e: 1,
f: 1
}
// Need to get 2,2,3 above. The first 3 highest ones.
let object2 = {
a: 7,
b: 15,
c: 1,
d: 7,
e: 4,
f: 3
}
// 15,7,7 above.
But it is not like return > 1 because I cannot set an exact number; the object can be
let object1 = {
a: 1,
b: 1,
c: 0,
d: 2,
e: 0,
f: 0
}
I have tried it like keys.reduce but I could not reach out to a good solution.
let input = {
a: 1,
b: 1,
c: 0,
d: 2,
e: 0,
f: 0
}
console.log(Object.values(input).sort((x, y) => y - x).slice(0, 3));
You can use sort against the values of the object. First use Object.values to change your object values into an array then run a sort against it with a slice to fetch first 3 highest values.
Object.values(input).sort((x, y) => y - x).slice(0, 3);