In this article, we are given an array of objects and the task is to get the maximum and minimum values from the array of objects. For this approach, we have a few methods that will be discussed below.
Methods to get Min/Max values:
- using JavaScript apply() and Array map()
- using JavaScript Array reduce() Method
- using JavaScript loops
Method 1: Using JavaScript apply() and Array map()
JavaScript apply() Method: This method is different from the function call() because of taking arguments as an array.Â
Syntax:Â
apply()
JavaScript Array map() Method: This method is used to create a new array with the results of calling a function for each element of the array. This method calls the given function once for each element in an array, in order.Â
Syntax:Â
array.map(function(cValue, index, arr), thisValue)
Example: This example gets the maximum value of the y property by using apply() and map() methods.Â
Javascript
let Arr = [    { "x": "3/10/2003", "y": 0.023452007 },    { "x": "8/12/2002", "y": 0.02504234 },    { "x": "1/16/2001", "y": 0.024533546 },    { "x": "8/19/2006", "y": 0.03123423457 }];Â
console.log(JSON.stringify(Arr));Â
function gfg_Run() {Â Â Â Â console.log("Maximum value of y = " +Â Â Â Â Â Â Â Â Math.max.apply(Math, Arr.map(function (event) {Â Â Â Â Â Â Â Â Â Â Â Â return event.y;Â Â Â Â Â Â Â Â })));}Â Â
gfg_Run(); |
[{"x":"3/10/2003","y":0.023452007},{"x":"8/12/2002","y":0.02504234},{"x":"1/16/2001","y":0.024533546},{"x":"8/19/2006","y":0.03123423457}]
Maximum value of y = 0.03123423457
Method 2: Using JavaScript Array reduce() Method
This method reduces the array to a single value. This method runs a defined function for every value of the array (from left to right). The return value of the function is stored in an accumulator (result or total).
Syntax:Â
array.reduce(function(total, curValue, curIndex, arr), initialValue)
Example: This example gets the minimum value of the y property by using the array.reduce() method. It returns the whole object.
Javascript
let Arr = [    { "x": "3/10/2003", "y": 0.023452007 },    { "x": "8/12/2002", "y": 0.02504234 },    { "x": "1/16/2001", "y": 0.024533546 },    { "x": "8/19/2006", "y": 0.03123423457 }];Â
console.log(JSON.stringify(Arr));Â
function gfg_Run() {    console.log(JSON.stringify(        Arr.reduce(function (prev, current) {            return (prev.y < current.y) ? prev : current        })));}Â
gfg_Run(); |
[{"x":"3/10/2003","y":0.023452007},{"x":"8/12/2002","y":0.02504234},{"x":"1/16/2001","y":0.024533546},{"x":"8/19/2006","y":0.03123423457}]
{"x":"3/10/2003","y":0.023452007}
Method 3: Using JavaScript loops
Example: In this example, we will use JavaScript for loop to get the min and max values from an array of objects
Javascript
// Input array of objectslet Arr = [    { "x": "3/10/2003", "y": 0.023452007 },    { "x": "8/12/2002", "y": 0.02504234 },    { "x": "1/16/2001", "y": 0.024533546 },    { "x": "8/19/2006", "y": 0.03123423457 }];Â
// Initialize min and max values let maxValue = Arr[0].y;let minValue = Arr[0].y;Â
// Getting min and max value using for loopsfor (let i = 0; i < Arr.length; i++) {Â Â if (Arr[i].y > maxValue) {Â Â Â Â maxValue = Arr[i].y;Â Â }Â Â if (Arr[i].value < minValue) {Â Â Â Â minValue = Arr[i].y;Â Â }}Â
// Display the outputconsole.log(Arr)console.log("Max Value:", maxValue);console.log("Min Value:", minValue); |
[
{ x: '3/10/2003', y: 0.023452007 },
{ x: '8/12/2002', y: 0.02504234 },
{ x: '1/16/2001', y: 0.024533546 },
{ x: '8/19/2006', y: 0.03123423457 }
]
Max Value: 0.03123423457
Min Value: 0.023452...
