Published: September 14 2021

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:

 


Need Some Vanilla JS Help?

Search fiverr for freelance Vanilla JS developers.


Follow me for updates

On Twitter or RSS.


When I'm not coding...

Me and Tina are on a motorcycle adventure around Australia.
Come along for the ride!


Comments


Supported by