- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sorting a JSON object in JavaScript
Suppose we have an object like this −
const obj = { key1: 56, key2: 67, key3: 23, key4: 11, key5: 88 };
We are required to write a JavaScript function that takes in this object and returns a sorted array like this −
const arr = [11, 23, 56, 67, 88];
Here, we sorted the object values and placed them in an array.
Therefore, let’s write the code for this function −
Example
The code for this will be −
const obj = { key1: 56, key2: 67, key3: 23, key4: 11, key5: 88 }; const sortObject = obj => { const arr = Object.keys(obj).map(el => { return obj[el]; }); arr.sort((a, b) => { return a - b; }); return arr; }; console.log(sortObject(obj));
Output
The output in the console will be −
[ 11, 23, 56, 67, 88 ]
- Related Articles
- Flattening a JSON object in JavaScript
- Constructing a nested JSON object in JavaScript
- JSON group object in JavaScript
- Removing property from a JSON object in JavaScript
- Print JSON nested object in JavaScript?
- Deep Search JSON Object JavaScript
- Converting two arrays into a JSON object in JavaScript
- How to convert JSON text to JavaScript JSON object?
- How to turn a JSON object into a JavaScript array in JavaScript ?
- How to parse JSON object in JavaScript?
- How to deserialize a JSON into Javascript object?
- Finding the smallest value in a JSON object in JavaScript
- Create array from JSON object JavaScript
- From JSON object to an array in JavaScript
- How to transform JSON text into a JavaScript object?

Advertisements