- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 can I instantiate a dictionary in JavaScript where all keys map to the same value?
At first, set the keys −
const name = ['Name1', 'Name2'];
Now, map keys to the same value using for loop as in the below code −
Example
const name = ['Name1', 'Name2']; const keyValueObject = {}; for (const k of name){ keyValueObject[k] = 'John'; } console.log(keyValueObject);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo48.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo48.js { Name1: 'John', Name2: 'John' }
- Related Articles
- JavaScript map value to keys (reverse object mapping)
- How to print all the keys of a dictionary in Python
- How to get a list of all the keys from a Python dictionary?
- How can I convert Python dictionary to JavaScript hash table?
- How to remove all the elements from a map in JavaScript?
- How to sort a dictionary in Python by keys?
- How to add new keys to a dictionary in Python?
- How do I see all foreign keys to a table column?
- In PHP, can you instantiate an object and call a method on the same line?
- How to map two lists into a dictionary in Python?
- How do I assign a dictionary value to a variable in Python?
- Building a Map from 2 arrays of values and keys in JavaScript
- Python – Limit the values to keys in a Dictionary List
- How can I convert a Python Named tuple to a dictionary?
- How to create Python dictionary with duplicate keys?

Advertisements