JavaScript Get Max Value In Array Of Objects

To get max value in array of objects in JavaScript –

  1. Get an array of key values using map() method.
  2. Find the maximum value in the array using Math.max() method. Math.max() takes a list of values as input and returns the maximum value of the array.
  3. For Internet Explorer, you can use the Math.max.apply() method.
  4. You can use the for loop to find the max value in an array of objects.
javascript get max value in an array of objects

JavaScript Get Max Value In Array Of Objects Using map() Method

The map() method is used to create a new array from the existing array. It calls the function for each element of the existing array and returns a new array with the return value of the function.

The following example shows how to get max value in an array of object using map() method.

var arr = [];

arr.push( { name: 'John', age: 20 },

{ name: 'Mike', age: 30 },

{ name: 'Peter', age: 40 } );

var ids = arr.map(function(a) {

return a.age;

});

console.log("Ids: "+ids);

var max = Math.max(…ids);

console.log(max);

Output:

Ids: 20,30,40

Max value: 40

We have used the spread operator (…) to convert the array ids into a list of arguments. Then we have passed this list as an argument to Math.max() method.

JavaScript Get Max Value In Array Of Objects Using Math.max.apply() Method

The apply() method calls a function with a given this value and arguments provided as an array.

For Internet Explorer, you can use the max.apply() method to get max value in an array of object, as the Math.max () method doesn’t support Internet Explorer.

The following example shows how to get max value in an array of object using Math.max.apply() method.

var arr = [];

arr.push( { name: 'John', age: 20 },

{ name: 'Mike', age: 30 },

{ name: 'Peter', age: 40 } );

var ids = arr.map(function(a) {

return a.age;

});

console.log("Ids: "+ids);

var max = Math.max.apply(null, ids);

console.log("Max value: "+max);

Output:

Ids: 20,30,40

Max value: 40

The Math.max.apply method takes two arugments, the first argument is ‘this’ value and the second argument is an array. The ‘this’ value is set to null as we don’t need it in this example.

JavaScript Get Max Value In Array Of Objects Using for loop

You can also use the for loop to get max value in an array of objects. The following example shows how to get max value in an array of object using for loop.

var arr = [];

arr.push( { name: 'John', age: 20 }, 

     { name: 'Mike', age: 30 }, 

     { name: 'Peter', age: 40 } ); 

var max = 0;  

for ( var i=0; i < arr.length; i++ ) { 

 if ( arr[i].age > max ) { 

   max = arr[i].age; 

 } 

}   

console.log("Max value: "+max); 

Output: Max value: 40

In the above example, we have created an array of objects named arr. Then we have initialized a variable max with value 0. We have iterated over all the objects in the array and checked if the age property is greater than max. If it is greater than max, then we have assigned that value to max.

Finally, we have printed the value of max on the console.

Conclusion

In this article, you have learned how to get max value in an array of objects in JavaScript. You can use the map() method or for loop to find the max value in an array of objects.

I hope this article will help you. Do comment if you have any doubt and suggestions on this topic. Thanks for reading!

Leave a Reply