Get The Min And Max Values In An Object In JavaScript

Get the Min and Max Values in an Object in JavaScript

We will learn how to get the Min and Max values in an object in JavaScript with two solutions. Each way is rather straightforward, and you can use whatever you like.

Get the Min and Max values in an object in JavaScript

Using sort() with Object.values()

If you want to get the Min and Max values, you can sort the object values from smallest to largest, you can call this method on an array of the object values and pass the sorting function to its argument.

Syntax:

sort(function)

Parameter:

  • function: write the function directly which you want to sort.

You should remember not to use the method immediately on an object. To do this, you need to have an array (by using Object.values) and then use that array to run the method to sort them by values. Finally, print out the first element and the last element in the array to get what you want.

First, you need to see how to convert the object into an array of values and the result after converting:

// Create an object
var object = {learn: 20, share: 2000, it: 2}

// Convert the object into array of values
var array = Object.values(object)

console.log(array)

Output: 

[20, 2000, 2]

The example below declared a new object to test the Object.value() function. As you can see, the result after converting is an array of the objects’ values; but it is not sorted correctly in the declared order. After that, we will use the sort() method and apply a sorting function to sort them in ascending order:

// Create an object
var object = {learn: 20, share: 2000, it: 2}

// Convert the object into array of values
var array = Object.values(object)

console.log(array)

// Make a sort function to sort in terms of ascending values order
var sortFunction = (a,b) => a - b

// Sort the array by the sort() function
console.log(array.sort(sortFunction))

 Output: 

[20, 2000, 2]
[2, 20, 2000]

As can be seen, the array has been sorted in ascending value order. It is the fact that the sort function has done the job and returns a value of the subtract expression a minus b. So what are they? They are two elements of an array to be compared. Suppose a is the first element and b is the second element. Therefore, a – b is equal to 20 – 2000. The returned value will be a negative number, so they will be swapped until every element appears from smallest to largest.
Finally, after we have sorted the array containing object’s values, we then print out the Min and Max values:

// Get the min value (get the first element)
console.log(array[0])

// Get the max value (get the last element)
console.log(array[array.length-1])

Output: 

2
2000

Using Object.values() with Math.max() and Math.min()

If you think the sorting step is redundant as you don’t want to do that, you can also use this method to get the Max and Min values in the objects directly. However, you must need an array of object’s values, such as the first solution. Hence, in this solution, we only show you how to get the first and Max values and neglect the step of converting an object’s values into an array. The syntax and usage of the Max and Min methods have been introduced in this post.

// Create an object
var object = {learn: 20, share: 2000, it: 2}

// Convert the object into array of values
var array = Object.values(object)
console.log(array)

// Get the min value
console.log(Math.min(...array))

// Get the max value
console.log(Math.max(...array))

Output: 

[ 20, 2000, 2 ]
2
2000

As you can see, this method does not require you to sort the array. Hence using it won’t affect your array. This is also considered the most effective way to get an object’s Min and Max values.

Summary

We have shown you how to get the Min and Max values in an object in JavaScript. It would help if you considered that the spread syntax may not work in some browsers. We wish you success with our tutorials.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *