Imagine an object like this:
var values = {
"2": 1,
"53": 2,
"56": 4,
"57": 9,
"61": 2,
"62": 16,
"63": 2,
"398": 24,
...
}
My goal is, to find the 10 object keys, which have the highest value. In this case: 398, then 62 and so on (= [398, 62, ...]
). I know how I can put this into an array, don't know how to receive the property key though.
Important: I can't change the format because it's a server response.
I tried with a for (key in values) {}
loop but have no idea how to move on.
This similar question and it's answer couldn't really help me either.
As commented before:
Object.keys(object)
sort((a,b)=> object[b] - object[a])
keys.slice(0,n)
You could sort by the values property and slice the result.
You can use
Object.keys
to get key valuesNow you can get highest top 10 keys by
sortedKeys.slice(0, 10);