
- 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
Find n highest values in an object JavaScript
Let’s say, we have an object that describes various qualities of a football player like this −
const qualities = { defence: 82, attack: 92, heading: 91, pace: 96, dribbling: 88, tenacity: 97, vision: 91, passing: 95, shooting: 90 };
We wish to write a function that takes in such object and a number n (n <= no. of keys in object) and returns an object with n highest key value pairs.
Like for n = 2
Output should be −
{ tenacity: 97, pace: 96 }
Therefore, let’s write the code for this function,
The complete code for this function will be −
Example
const qualities = { defence: 82, attack: 92, heading: 91, pace: 96, dribbling: 88, tenacity: 97, vision: 91, passing: 95, shooting: 90 }; const pickHighest = (obj, num = 1) => { const requiredObj = {}; if(num > Object.keys(obj).length){ return false; }; Object.keys(obj).sort((a, b) => obj[b] - obj[a]).forEach((key, ind) => { if(ind < num){ requiredObj[key] = obj[key]; } }); return requiredObj; }; console.log(pickHighest(qualities, 3));
Output
The output in the console will be −
{ tenacity: 97, pace: 96, passing: 95 } { tenacity: 97 } { tenacity: 97, pace: 96, passing: 95, attack: 92, heading: 91 }
- Related Articles
- How to return object from an array with highest key values along with name - JavaScript?
- How to find inside an array of objects the object that holds the highest value in JavaScript?
- Highest and lowest in an array JavaScript
- Converting a JavaScript object to an array of values - JavaScript
- Returning the highest number from object properties value – JavaScript
- How to get the values of an object in JavaScript?
- Retrieve key and values from object in an array JavaScript
- Get the max n values from an array in JavaScript
- How to set default values when destructuring an object in JavaScript?
- Find the highest 3 values in a dictionary in Python program
- Returning the highest value from an array in JavaScript
- Add matching object values in JavaScript
- Python program to find the highest 3 values in a dictionary
- Highest occurrence in an array or first selected in JavaScript
- How to edit values of an object inside an array in a class - JavaScript?

Advertisements