# Find the max or min value from an array or an object

As JavaScript Developer we usually need to work on arrays & array objects and sometimes we need to do some operations on arrays like searching, sorting, find min/max etc.

## So today's post we will see how to find max or min value from an array.

So let's take an example of the following array.

`let arr = [21,32,43,34,42,5,6,2,43,23];`

If we want to find the maximum value from the array, so code will be as follows:

**Math.max(…arr);**

So this will provide the maximum value from the array element.

**ProTip:**

*If you are not aware of ... the operator then just inline explanation is that this is Destructuring mechanism for an array so it provides values in the destructed format.*

### If we want to find the minimum value from the array, so code will be as follows:

**Math.min(…arr);**

*So this will provide the minimum value from the array element.*If we print it then the output will be as follows:

**> Output: 2**

## Now we will work on an array of Object.

### Javascript get max value in an array of objects.

So now if you have an array of object and you wanted to get the max value from it then the following example will help you.

**let objArr = [{name:'John',age:23},{name:'Rock',age:28},{name:'Tom',age:21}]**

And answer will be as follows:

**const maxValue = Math.max(…objArr.map(val => val.age), 0);**

So in the above example, we used the following methods:

Math.max() is a method to find the max value from the given elements.

Next is **…objArr.map(val => val.age) is used to destruct the array into element with mapping the age only. so it will print as follows:**

And finally, by using Math.max() we find maximum value from the values and returned it.

That's it, I hope you got your answer. if you want to read more about mastering in javascript then please check this link.

## No comments:

## Post a comment