- 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
Creating Dictionary using Javascript
Let's create a MyMap class so that it doesn't hide the actual Map class in JS. We'll create a container object that'll keep track of all our values that we add to the map. We'll also create a display function that prints the map for us.
Example
class MyMap { constructor() { this.container = {}; } display() { console.log(this.container); } }
In ES6, you can directly create a dictionary using the Map class. For example,
Example
const map1 = new Map(); const map2 = new Map([ ["key1", "value1"], ["key2", "value2"] ]);
Checking if a key existing
We need to define the hasKey method so that we can check if a key is already there. We will use this method while removing elements and setting new values.
Exaample
hasKey(key) { return key in this.container; }
In ES6, you can check if a key exists in a map using the has method. For example,
Example
const myMap = new Map([ ["key1", "value1"], ["key2", "value2"] ]); console.log(myMap.has("key1")) console.log(myMap.has("key3"))
Output
This will give the output −
True False
- Related Articles
- Creating Arrays using Javascript
- Creating a Set using Javascript
- Creating a BinaryTree using Javascript
- Clearing a Dictionary using Javascript
- Creating and Maintaining a WBS Dictionary
- Creating a Priority Queue using Javascript
- Creating a linked list using Javascript
- Creating a hash table using Javascript
- Creating multiple boxplots on the same graph from a dictionary, using Matplotlib
- Creating a Doubly Linked List using Javascript
- Creating JavaScript constructor using the “new” operator?
- Creating auto-resize text area using JavaScript
- Remove elements from a Dictionary using Javascript
- Search Element in a Dictionary using Javascript
- Creating Animated Counter using HTML, CSS, and JavaScript

Advertisements