I'm trying to sort an array of objects by its values and return the 3 keys with the highest value in descending order. The array is:
let obj = [
{'tom':4},
{'bill':5},
{'tina':6},
{'tim': 3}]
Solution I'm looking for: ['tina', 'bill', 'tom']
In case, there are more than three values, that fulfill the condition I would like to list them as well, like:
let obj = [
{'tom':4},
{'bill':5},
{'tina':6},
{'tim': 3},
{'jim':4]
Solution I'm looking for: ['tina', 'bill', 'tom', 'jim']
I tried to do something like:
Object.entries(obj).sort((a,b) => b[1] - a[1]).map(value => ({[value[0]]: value[1]}))
but couldn't figure out the right approach.