
- 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
Retrieving object's entries in order with JavaScript?
Let’s say the following is our object −
const subjectDetails ={ 102:"Java", 105:"JavaScript", 104:"MongoDB", 101:"MySQL" };
Use sort() method to retrieve object’s entry in order −
const orderSubjects = Object.fromEntries(Object.keys(subjectDetails).sort().map((k) => { return [k, subjectDetails[k]]; }));
Example
const subjectDetails ={ 102:"Java", 105:"JavaScript", 104:"MongoDB", 101:"MySQL" }; const orderSubjects = Object.fromEntries(Object.keys(subjectDetails).sort().map((k) => { return [k, subjectDetails[k]]; })); console.log(orderSubjects);
To run the above program, you need to use the following command −
node fileName.js.
Output
Here, my file name is demo146.js. This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo146.js{ '101': 'MySQL', '102': 'Java', '104': 'MongoDB', '105': 'JavaScript' }
- Related Articles
- Split array entries to form object in JavaScript
- Retrieving n smallest numbers from an array in their original order in JavaScript
- Is it possible to display substring from object entries in JavaScript?
- Sorting numbers in descending order but with `0`s at the start JavaScript
- Count number of entries in an object having specific values in multiple keys JavaScript
- Retrieving group by result with arrays in MongoDB?
- Combining the identical entries together in JavaScript
- How to manipulate JavaScript's Date object?
- Retrieving the decimal part only of a number in JavaScript
- Grouping identical entries into subarrays - JavaScript
- Retrieving an embedded object as a document via the aggregation framework in MongoDB?
- How to access an object having spaces in the object’s key using JavaScript?
- Retrieving Cookies in Python
- Binding an object's method to a click handler in JavaScript
- Matplotlib histogram with multiple legend entries

Advertisements