- 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
How to use my object like an array using map function in JavaScript?
For this, use Object.keys() as well as Object.values() and map() for the result.
Example
const object = { name: 'John', age: 21, countryName: 'US', subjectName: 'JavaScript' } const allKeysOfObject = Object.keys(object); console.log("The all keys are=" + allKeysOfObject); const allValues = Object.values(object); console.log("The all values are=" + allValues); console.log("The use of map is as follows="); allKeysOfObject.map(k => { console.log(object[k]) })
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo185.js.
Output
This will produce the following output −
PS C:\Users\Amit\javascript-code> node demo185.js The all keys are=name,age,countryName,subjectName The all values are=John,21,US,Javascript The use of map is as follows= John 21 US JavaScript
Advertisements