0

I'm extremely new to JS, so I'm struggling on how to even ask the question, but here it goes:

I have a dict: let x = {a:12,b:5,c:1,d:7,e:10}; Values change but keys remain the same.

I need a way to produce output of highest 3 keys based on their values, something like this: let gold = "a" let silver = "e" let bronze = "d"

Any help is appreciated. Thank you.

Marko Tankosic
  • 164
  • 2
  • 12
  • Sort descending then https://stackoverflow.com/questions/34883068/how-to-get-first-n-number-of-elements-from-an-array – mplungjan Feb 07 '18 at 06:45
  • i think that first you better to convert your object to array:Object.keys(obj).map((a)=>({id:a, value:obj[a]})); after you can sort it and select first 3 : Object.keys(obj).map((a)=>({id:a, value:obj[a]})).sort((a,b)=>a.value – happyZZR1400 Feb 07 '18 at 06:55
  • @happyZZR1400 - Thank you very, very much, that worked like a charm! – Marko Tankosic Feb 07 '18 at 08:57

0 Answers0