Published:

Vanilla JS - Get the max prop value from an object array in JavaScript

This is a quick example of how to get the maximum value of a property in an array of objects in JavaScript.

The below code snippet uses the following three Vanilla JS tools to get the max prop value from an array:

  • the Array.map() function to create a new array that contains only the property we're interested in (.value)
  • the ES6 Spread Operator (...) to expand/convert the array into a list of arguments that can be passed to a function as input parameters
  • the Math.max() function to get the largest value from the input parameters
const arrayOfObjects = [
    { value: 1 },
    { value: 2 },
    { value: 3 },
    { value: 4 },
    { value: 5 }
];
const maxValue = Math.max(...arrayOfObjects.map(x => x.value));

Run the example vanilla JS on StackBlitz at https://stackblitz.com/edit/vanilla-js-get-max-prop-value-from-object-array.

More info on JavaScript functions used

For more info on the Vanilla JS functions and operator used in the example see the below links:

 


Subscribe or Follow Me For Updates

Subscribe to my YouTube channel or follow me on Twitter, Facebook or GitHub to be notified when I post new content.

Other than coding...

I'm currently attempting to travel around Australia by motorcycle with my wife Tina on a pair of Royal Enfield Himalayans. You can follow our adventures on YouTube, Instagram and Facebook.


Need Some Vanilla JS Help?

Search fiverr to find help quickly from experienced Vanilla JS developers.



Supported by