- 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
Un-nesting array of object in JavaScript?
To un-nest array of objects, use the concept of map(). Let’s say the following are our array of objects −
const studentDetails = [ { "studentId": 101, "studentName": "John", "subjectDetails": { "subjectName": "JavaScript" } }, { "studentId": 102, "studentName": "David", "subjectDetails": { "subjectName": "MongoDB" } } ];
We need to un-nest the subjectName and display the result. Following is the code −
Example
const studentDetails = [ { "studentId": 101, "studentName": "John", "subjectDetails": { "subjectName": "JavaScript" } }, { "studentId": 102, "studentName": "David", "subjectDetails": { "subjectName": "MongoDB" } } ]; const output = studentDetails.map(obj => ({ studentId: obj.studentId, studentName: obj.studentName, subjectName:obj.subjectDetails.subjectName })); console.log(output);
To run the above program, you need to use the following command −
node fileName.js.
Output
Here, my file name is demo92.js. This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo92.js [ { studentId: 101, studentName: 'John', subjectName: 'JavaScript' }, { studentId: 102, studentName: 'David', subjectName: 'MongoDB' } ]
- Related Articles
- Array Nesting in C++
- Nesting template strings in JavaScript
- JavaScript - How to create nested unordered list based on nesting of array?
- Convert array of object to array of array in JavaScript
- Convert object of objects to array in JavaScript
- Replacing array of object property name in JavaScript
- Convert object to array of objects in JavaScript
- Nesting Columns in Bootstrap
- Object to array - JavaScript
- Sum of array object property values in new array of objects in JavaScript
- Sum of nested object values in Array using JavaScript
- Converting array of objects to an object in JavaScript
- Converting array of arrays into an object in JavaScript
- Sort object array based on another array of keys - JavaScript
- Transforming array to object JavaScript

Advertisements