Skip to content

Instantly share code, notes, and snippets.

@koozdra
Created October 2, 2019 23:20
Show Gist options
  • Save koozdra/45e4d0d9be58d6b9d37c67fe5ddfcc92 to your computer and use it in GitHub Desktop.
Save koozdra/45e4d0d9be58d6b9d37c67fe5ddfcc92 to your computer and use it in GitHub Desktop.
Get all values that have a max value under a getter
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