
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to return object from an array with highest key values along with name - JavaScript?
Suppose, we have an array of objects that contains information about marks of some students in a test −
const students = [ { name: 'Andy', total: 40 }, { name: 'Seric', total: 50 }, { name: 'Stephen', total: 85 }, { name: 'David', total: 30 }, { name: 'Phil', total: 40 }, { name: 'Eric', total: 82 }, { name: 'Cameron', total: 30 }, { name: 'Geoff', total: 30 } ];
We are required to write a JavaScript function that takes in one such array and returns a object with the name and total of the student that have highest value for total.
Therefore, for the above array, the output should be −
{ name: 'Stephen', total: 85 } 
Example
Following is the code −
const students = [ { name: 'Andy', total: 40 }, { name: 'Seric', total: 50 }, { name: 'Stephen', total: 85 }, { name: 'David', total: 30 }, { name: 'Phil', total: 40 }, { name: 'Eric', total: 82 }, { name: 'Cameron', total: 30 }, { name: 'Geoff', total: 30 } ]; const pickHighest = arr => { const res = { name: '', total: -Infinity }; arr.forEach(el => { const { name, total } = el; if(total > res.total){ res.name = name; res.total = total; }; }); return res; }; console.log(pickHighest(students));
Output
This will produce the following output on console −
{ name: 'Stephen', total: 85 }
- Related Articles
- Retrieve key and values from object in an array JavaScript
- Sum from array values with similar key in JavaScript
- How to modify key values in an object with JavaScript and remove the underscore?
- How to merge an array with an object where values are arrays - JavaScript
- How to get key name when the value contains empty in an object with JavaScript?
- Find n highest values in an object JavaScript
- Looping numbers with object values and push output to an array - JavaScript?
- JavaScript: Combine highest key values of multiple arrays into a single array
- How to return an object from a JavaScript function?
- JavaScript: How to Create an Object from Key-Value Pairs
- How to return an array whose elements are the enumerable property values of an object in JavaScript?
- How to Declare an Object with Computed Property Name in JavaScript?
- How to create an array with random values with the help of JavaScript?
- JavaScript: replacing object keys with an array
- Filter away object in array with null values JavaScript

Advertisements