Created
March 28, 2018 20:00
-
-
Save misterussell/76b4cc654dd005d9dbd8f1245a293239 to your computer and use it in GitHub Desktop.
Find's the highest values in an array and return an object with their indices as keys.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findHighestValues(arr, numberOfValues) { | |
return Object.assign(...arr.map((val, i) => [i, val]) | |
.sort((a, b) => b[1] - a[1]) | |
.filter((keyVal, i) => i <= numberOfValues) | |
.map(([i, val]) => ({[i]: val}))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment