JavaScript: Get the Min/Max Values in an Object

Published : 2nd March 2022 | by @faisaljebali
#javascript#object#min#max

In this article I will show you how to get max/min value in an object.

To get the min and max values in an Object:

  1. Use the Object.values method to get an array of the object's values.

  2. Using spread syntax, pass the array to the Math.min() and Math.max() methods.

  3. The Math.min and Math.max methods return the lowest and highest of the passed in numbers.

const obj = {num1: 10, num2: 20, num3: 5, num4: 15}; const values = Object.values(obj);

console.log(values); // 👉️ [10, 20, 5, 15] const max = Math.max(...values);

console.log(max); // 👉️ 20 const min = Math.min(...values);

console.log(min); // 👉️ 5

Home page