- 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
Creating a JavaScript Object from Single Array and Defining the Key Value?
To convert a JavaScript object to key value, you need to use Object.entries() along with map(). Following is the code −
Example
var studentObject={ 101: "John", 102: "David", 103: "Bob" } var studentDetails = Object.assign({}, studentObject) studentDetails = Object.entries(studentObject).map(([studentId,studentName])=>({studentId ,studentName})); console.log(studentDetails);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo113.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo113.js [ { studentId: '101', studentName: 'John' }, { studentId: '102', studentName: 'David' }, { studentId: '103', studentName: 'Bob' } ]
Advertisements