
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Instantiate Dictionary in JavaScript with Same Value for All Keys
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' }
Advertisements