How to search the max value of an attribute in an array object ?
Last Updated :
27 Dec, 2023
In this article, we will learn how to search for the maximum 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.
These are the following methods:
Approach 1: Using Loop
In this approach, the array is traversed and the required values of the object are compared for each index of the array.
Example: This example shows the implementation of the above-explained approach.
javascript
let arr = [
{
a: 10,
b: 25
},
{
a: 30,
b: 5
},
{
a: 20,
b: 15
},
{
a: 50,
b: 35
},
{
a: 40,
b: 45
},
];
function fun(arr) {
let maxValue = Number.MIN_VALUE;
for (let i = 0; i < arr.length; i++) {
if (arr[i].a > maxValue) {
maxValue = arr[i].a;
}
}
return maxValue;
}
let maxValue = fun(arr);
console.log(maxValue);
|
In this approach, we find the max value of an attribute by using Math.max.apply() function.
Syntax:
Math.max.apply(thisArg, [ argsArray])
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: This example shows the implementation of the above-explained approach.
javascript
let arr = [
{
a: 10,
b: 25
},
{
a: 30,
b: 5
},
{
a: 20,
b: 15
},
{
a: 50,
b: 35
},
{
a: 40,
b: 45
},
];
let maxValue = Math.max.apply( null ,
arr.map( function (o) { return o.a; }));
console.log(maxValue);
|
Approach 3: Using reduce() method
In this approach, 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.
Example: This example shows the implementation of the above-explained approach.
Javascript
let array = [
{ a: 1, b: 2 },
{ a: 2, b: 4 },
{ a: 3, b: 6 },
{ a: 4, b: 8 },
{ a: 5, b: 10 },
{ a: 6, b: 12 },
];
let maxValue = array.reduce((acc, value) => {
return (acc = acc > value.b ? acc : value.b);
}, 0);
console.log(maxValue);
|
In this approach, we are using third party librray that is lodash. lodash provides methods for arrays and objects here we are using _.sortBy() method that sort the arrays of object in ascending order. we will get our sorted array of objects by using this method after that we can directlly access the last element if that array as it will be the maximum.
Example: This example shows the implementation of the above-explained approach.
Javascript
let arr = [
{
a: 10,
b: 25
},
{
a: 30,
b: 5
},
{
a: 20,
b: 15
},
{
a: 50,
b: 35
},
{
a: 40,
b: 45
},
];
const _ = require( "lodash" );
let sorted_obj = _.sortBy(arr,
[ function (o) { return o.a; }]);
console.log(sorted_obj[sorted_obj.length-1]);
|
Output:
{ a: 50, b: 35 }
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...