

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 find inside an array of objects the object that holds the highest value in JavaScript?
We have an array that holds several objects named student, each object student has several properties, one of which is an array named grades −
const arr = [ { name: "Student 1", grades: [ 65, 61, 67, 70 ] }, { name: "Student 2", grades: [ 50, 51, 53, 90 ] }, { name: "Student 3", grades: [ 0, 20, 40, 60 ] } ];
We need to create a function that loops through the student's array and finds which student object has the highest grade inside its grades array.
Example
The code for this will be −
const arr = [ { name: "Student 1", grades: [ 65, 61, 67, 70 ] }, { name: "Student 2", grades: [ 50, 51, 53, 90 ] }, { name: "Student 3", grades: [ 0, 20, 40, 60 ] } ]; const highestGrades = arr.map((stud, ind) => { return { name: stud.name, highestGrade: Math.max.apply(Math, stud.grades) // get a student's highest grade }; }); const bestStudent = highestGrades.sort((a, b) => { return b.highestGrade − a.highestGrade; })[0]; console.log(bestStudent.name + " has the highest score of " + bestStudent.highestGrade);
Output
And the output in the console will be −
Student 2 has the highest score of 90
- Related Questions & Answers
- Returning the highest value from an array in JavaScript
- Converting array of objects to an object of objects in JavaScript
- Group objects inside the nested array JavaScript
- Parsing array of objects inside an object using maps or forEach using JavaScript?
- Find n highest values in an object JavaScript
- Converting array of objects to an object in JavaScript
- How to find the maximum value of an array in JavaScript?
- How to find the minimum value of an array in JavaScript?
- How to transform object of objects to object of array of objects with JavaScript?
- Splitting an object into an array of objects in JavaScript
- Find the closest value of an array in JavaScript
- How to insert an item to an array that is inside an object in MongoDB?
- Convert array of objects to an object of arrays in JavaScript
- Insert value in the middle of every value inside array JavaScript
- How to edit values of an object inside an array in a class - JavaScript?
Advertisements