
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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' } ]
- Related Articles
- Creating an array using a string which contains the key and the value of the properties - JavaScript
- Extract key value from a nested object in JavaScript?
- Retrieve key and values from object in an array JavaScript
- Get value for key from nested JSON object in JavaScript
- JavaScript: How to Create an Object from Key-Value Pairs
- JavaScript - Sort key value pair object based on value?
- Creating String Object from Character Array in Java
- Top n max value from array of object JavaScript
- Get key from value in JavaScript
- Get max value per key in a JavaScript array
- JavaScript creating an array from JSON data?
- JavaScript: Combine highest key values of multiple arrays into a single array
- JavaScript: How to remove the key-value pairs corresponding to the given keys from an object?
- How to store a key => value array in JavaScript?
- JavaScript - Convert an array to key value pair

Advertisements