Register for 45 Day Coding Challenge by CodeStudio And Win Some Exciting Prizes

# JavaScript Get Highest Value in Array of Objects

JavaScript Get Highest Value in Array Of Objects | The Array object, like arrays in other computer languages, allows you to store a collection of several things under a single variable id and includes members for typical array operations.

Use the built-in method in JavaScript, use apply(), map(), and reduce() to get the highest value in an array of objects. You can store numerous values in a single variable using the array object.

Methods for obtaining the highest value in an array of objects include:-

1.  Using the map() and apply() method
2.  The spread operator, map(), and Math

## JavaScript Get Highest Value in Array of Objects using map()

1. The map() method returns an array of ids.
2. Null and the array of ids are passed as inputs to the apply function on Math.max. The highest of the given numbers is returned by the Math.max method.

We employ the Function.apply method rather than the… operator. The method receives the following two arguments:

1. The “this” argument is useless for our objectives.
2. Math.max() will receive an array of numbers from the apply method as arguments.

``````const array = [{ id: 12 }, { id: 45 }, { id: 34 }, { id: 144 }];

const id = array.map(object => {
return object.id;
});
console.log(id);

const max = Math.max.apply(null, id);
console.log(max);``````

Output:-

[ 12, 45, 34, 144 ]
144

Program to get max value from array of objects JavaScript using map() with Negative inputs

``````const array = [{ id: -12 }, { id: -45 }, { id: 34 }, { id: -144 }];

const id = array.map(object => {
return object.id;
});
console.log(id);

const max = Math.max.apply(null, id);
console.log(max);``````

Output:-

[ -12, -45, 34, -144 ]
34

## Get Max Value From Array of Objects JavaScript using Spread Operator, map(), and Math

1. The map() method returns an array of ids.
2.  As an argument, give the array of ids prefixed by the spread operator to the Math.max method. Max value from an array of objects in JavaScript is returned by the Math.max method.

To acquire an array containing the ids of all objects, we utilize the Array.map method in the given code. The spread operator is then used to call the Math.max method.

We can’t just send an array to the Math.max method because it demands comma-separated values as inputs. To overcome this, we utilize the Spread operator to unpack the array’s values.

``````const array = [{ id: 23 }, { id: 7 }, { id: 34 }, { id: 53 }];

const id = array.map(object => {
return object.id;
});
console.log(id);

const max = Math.max(...id);
console.log(max); ``````

Output:-

[ 23, 7, 34, 53 ]
53

Program for JavaScript get highest value in array of Objects using spread operator, map(), and Math by taking Negative inputs

``````const array = [{ id: -24 }, { id: -04 }, { id: -22 }, { id: -17 }];

const id = array.map(object => {
return object.id;
});
console.log(id);

const max = Math.max(...id);
console.log(max); ``````

Output:-

[ -24, -4, -22, -17 ]
-4

Thus we learned how to get the highest value in an array of objects in JavaScript. Also see:- JavaScript get highest value in Array

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!