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

Updated on: 14-Sep-2020

88 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements