Find max and min entries of a JavaScript object

Let's assume that you have the following object:

javascript
let age = {
    'Erik': 31,
    'Bezos': 57,
    'Gates': 66,
    'Elon': 50 
}

You can use a reduce function to find the object's max and min entries.

Example:

javascript
let max = Object.entries(age).reduce((max, entry) => entry[1] >= max[1] ? entry : max, [0, -Infinity])
let min = Object.entries(age).reduce((min, entry) => entry[1] <= min[1] ? entry : min, [0, +Infinity])

Here are the results:

javascript
console.log(max) // ["Gates", 66]
console.log(min) // ["Erik", 31]

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.