Last active
January 24, 2022 17:50
-
-
Save osorionicolas/7827b14fd1234397884171e34f016a0c to your computer and use it in GitHub Desktop.
Find maximum item of an Array by given key
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 people = [ | |
| { name: 'Naruto', age: 24 }, | |
| { name: 'Sasuke', age: 32 }, | |
| { name: 'Minato', age: 42 }, | |
| { name: 'Kakashi', age: 36 } | |
| ] | |
| const maxBy = (arr, key) => arr.reduce((max,obj) => { | |
| return max[key] >= obj[key] ? max : obj) | |
| }, {}) | |
| maxBy(people, 'age') | |
| // output: { name: 'Minato', age: 42 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment