Skip to content

Instantly share code, notes, and snippets.

@misterussell
Created March 28, 2018 20:00
Show Gist options
  • Save misterussell/76b4cc654dd005d9dbd8f1245a293239 to your computer and use it in GitHub Desktop.
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.
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