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:
- Array map function - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
- ES6 spread operator - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
- Math max function - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max
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.
- Subscribe on YouTube at https://www.youtube.com/JasonWatmore
- Follow me on Twitter at https://twitter.com/jason_watmore
- Follow me on Facebook at https://www.facebook.com/JasonWatmoreBlog
- Follow me on GitHub at https://github.com/cornflourblue
- Feed formats available: RSS, Atom, JSON
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.
- Subscribe on YouTube at https://www.youtube.com/TinaAndJason
- Follow us on Instagram at https://www.instagram.com/tinaandjason
- Follow us on Facebook at https://www.facebook.com/TinaAndJasonVlog
- Visit our website at https://tinaandjason.com.au
Need Some Vanilla JS Help?
Search fiverr to find help quickly from experienced Vanilla JS developers.