- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to search the max value of an attribute in an array object ?
When working with objects, we often need to search for the max value of an attribute in an array of objects. We can use various javascript inbuilt functions like reduce() method, and map() method and we can also use a simple for loop to search for the max value. In this article, we will explore all these methods and explain the methods with the help of examples.
Method 1:Using for loops
In this method, we iterate through the array of objects using a loop and compare the attribute value of each object with the current maximum value.
Syntax
for (let i = 1; i < array.length; i++) { if (array[i].attribute > max) { max = array[i].attribute; }
Here, inside the loop, we compare the attribute value of each object with the current maximum (max). If a higher value is found, we update the max variable accordingly.
Example
In the below example, we have an array of student objects, where each object represents a student with a name and a score attribute. The loop compares the score attribute of each student with the current maximum maxScore. After iterating through all the students, the loop identifies that Alice has the highest score (95), and the output reflects this maximum score.
const students = [ { name: "John", score: 80 }, { name: "Alice", score: 95 }, { name: "Bob", score: 70 }, ]; let maxScore = students[0].score; for (let i = 1; i < students.length; i++) { if (students[i].score > maxScore) { maxScore = students[i].score; } } console.log("Maximum score:", maxScore);
Output
Maximum score: 95
Method 2:Using the reduce() Method
The reduce() method is applied to the array of objects, comparing the attribute value of each object with the previous maximum value and returning the object with the higher attribute value.
Syntax
const max = array.reduce((prev, current) => (prev.attribute > current.attribute) ? prev : current);
Here, the reduce() method allows us to iterate through an array and apply a reduction operation. In this case, we use reduce() to compare the attribute value of each object with the previous maximum value and return the object with the higher attribute value.
Example
Here, the reduce() method allows us to iterate through an array and apply a reduction operation. In this case, we use reduce() to compare the attribute value of each object with the previous maximum value and return the object with the higher attribute value.
const students = [ { name: "John", score: 80 }, { name: "Alice", score: 95 }, { name: "Bob", score: 70 }, ]; const maxScore = students.reduce((prev, current) => (prev.score >: current.score) ? prev : current); console.log("Maximum score:", maxScore.score);
Output
Maximum score: 95
Method 3:Using the Math.max() Method with map()
This method uses the map() method to extract the attribute values into a new array, which is then spread using the spread operator (...), and finally, the Math.max() method is applied to find the maximum value among those attribute values.
Syntax
const max = Math.max(...array.map(obj => obj.attribute));
Here, we use the map() method to extract the attribute values into a new array, and then apply the spread operator (...) along with Math.max() to find the maximum value among those attribute values.
Example
In the below example, we use map() to create a new array containing only the score attribute values from the students array. Then, we apply the spread operator (...) along with Math.max() to find the maximum value in that new array. The result is the maximum score, which is 95.
const students = [ { name: "John", score: 80 }, { name: "Alice", score: 95 }, { name: "Bob", score: 70 }, ]; const maxScore = Math.max(...students.map(obj => obj.score)); console.log("Maximum score:", maxScore);
Output
Maximum score: 95
Conclusion
In this article, we discussed how we can search for the maximum value of an attribute in an array object using different methods in Javascript. By using the loops, the reduce() method, or the combination of map() and Math.max(), we can easily find the maximum value of a specific attribute within an array of objects. You can use any of the methods depending the problem requirements.