
- 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
Returning the highest number from object properties value – JavaScript
Suppose, we have an object that contains rating of a property over some criteria like this −
const rating = { "overall": 92, "atmosphere": 93, "cleanliness": 94, "facilities": 89, "staff": 94, "security": 92, "location": 88, "valueForMoney": 92 }
We are required to write a JavaScript function that takes in one such object and returns the key value pair that has the highest value.
For example, for this very object, the output should be −
const output = { "staff": 94 };
Example
Following is the code −
const rating = { "overall": 92, "atmosphere": 93, "cleanliness": 94, "facilities": 89, "staff": 94, "security": 92, "location": 88, "valueForMoney": 92 } const findHighest = obj => { const values = Object.values(obj); const max = Math.max.apply(Math, values); for(key in obj){ if(obj[key] === max){ return { [key]: max }; }; }; }; console.log(findHighest(rating));
Output
This will produce the following output in console −
{ cleanliness: 94 }
- Related Articles
- Returning the highest value from an array in JavaScript
- Remove number properties from an object JavaScript
- Returning only odd number from array in JavaScript
- Extract properties from an object in JavaScript
- JavaScript Object Properties
- Returning the nth even number using JavaScript
- Accessing and returning nested array value - JavaScript?
- Returning Value from a Subroutine in Perl
- Returning number with increasing digits. in JavaScript
- Returning the expanded form of a number in JavaScript
- How to find inside an array of objects the object that holds the highest value in JavaScript?
- Find n highest values in an object JavaScript
- Returning values from a constructor in JavaScript?
- Returning the value of nth power of iota(i) using JavaScript
- How to duplicate Javascript object properties in another object?

Advertisements