How to search the max value of an attribute in an array object ?
The maximum value of an attribute in an array of objects can be searched in two ways, one by traversing the array and the other method by using the Math.max.apply() method.
Example 1: In this example, the array is traversed and the required values of the object are compared for each index of the array.
Example 2: In this example, we find the max value of an attribute by using Math.max.apply() function.
Math.max.apply(thisArg, [ argsArray])
Parameters: It has two parameters:
- thisArg: This argument is used to provide value for the call to the function.
- argsArray: It is an optional parameter. This is an array object used for specifying arguments with which function should be called.
Example 3: In this example, we will use reduce() method with which all the values will be compared, and then, at last, the final value will be stored which further will be stored in a variable that will be output over the console.