

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert object of objects to array in JavaScript
Let’s say we have the following object of objects that contains rating of some Indian players, we need to convert this into an array of objects with each object having two properties namely name and rating where name holds the player name and rating holds the rating object −
Following is our sample object −
const playerRating = { 'V Kohli':{ batting: 99, fielding: 99 }, 'R Sharma':{ batting: 98, fielding: 95 }, 'S Dhawan':{ batting: 92, fielding: 90 } }
The solution to this is quite simple and straightforward, we will use the Object.keys() method to iterate over the object simultaneously converting it into an array like this.
Following is the complete code with output
Example
const playerRating = { 'V Kohli':{ batting: 99, fielding: 99 }, 'R Sharma':{ batting: 98, fielding: 95 }, 'S Dhawan':{ batting: 92, fielding: 90 } } const objArray = []; Object.keys(playerRating).forEach(key => objArray.push({ name: key, rating: playerRating[key] })); console.log(objArray);
Output
[ { name: 'V Kohli', rating: { batting: 99, fielding: 99 } }, { name: 'R Sharma', rating: { batting: 98, fielding: 95 } }, { name: 'S Dhawan', rating: { batting: 92, fielding: 90 } } ]
- Related Questions & Answers
- Convert object to array of objects in JavaScript
- Convert array of objects to an object of arrays in JavaScript
- Convert an array of objects into plain object in JavaScript
- Converting array of objects to an object of objects in JavaScript
- How to transform object of objects to object of array of objects with JavaScript?
- Convert array of object to array of array in JavaScript
- Converting array of objects to an object in JavaScript
- Convert array of arrays to array of objects grouped together JavaScript
- Convert string with separator to array of objects in JavaScript
- JavaScript Converting array of objects into object of arrays
- Flat a JavaScript array of objects into an object
- JavaScript: Sort Object of Objects
- How to convert array to object in JavaScript
- Sum of array object property values in new array of objects in JavaScript
- Splitting an object into an array of objects in JavaScript
Advertisements