- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
What is the simplest solution to flat a JavaScript array of objects into an object?
Flat array of objects into an object, you can use the concept of reduce(). Let’s say following is our array of objects −
const studentDetails = [ {Name: "Chris"}, {Age: 22} ]
Example
const studentDetails = [ {Name: "Chris"}, {Age: 22} ] const objectStudent = studentDetails.reduce((obj, value) => { return { ...obj, ...value } }, {}) console.log(objectStudent);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo64.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo64.js { Name: 'Chris', Age: 22 }
Advertisements