Created
October 2, 2019 23:20
-
-
Save koozdra/45e4d0d9be58d6b9d37c67fe5ddfcc92 to your computer and use it in GitHub Desktop.
Get all values that have a max value under a getter
This file contains hidden or 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
const a = [ | |
{ a: 'one', b: 4 }, | |
{ a: 'two', b: 1 }, | |
{ a: 'three', b: 4 }, | |
{ a: 'four', b: 2 } | |
]; | |
function argmaxAll(getterFunction, arr) { | |
return reduce( | |
([max, arr], curr) => { | |
const currVal = getterFunction(curr); | |
if (currVal > max) { | |
return [currVal, [curr]]; | |
} | |
if (currVal === max) { | |
return [currVal, [...arr, curr]]; | |
} | |
return [max, arr]; | |
}, | |
[0, []] | |
)(arr)[1]; | |
} | |
console.log(argmaxAll(get('b'), a)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment