Clearing a Dictionary using Javascript


We'll implement a clear() function that simply clears the contents of the container. For example, 

Example

clear() {
   this.container = {}
}

You can test this using − 

Example

const myMap = new MyMap();
myMap.put("key1", "value1");
myMap.put("key2", "value2");
 
myMap.display();
myMap.clear();
myMap.display();

Output

This will give the output −

{ key1: 'value1', key2: 'value2' }

You can use the clear method in the same way in ES6 maps as well. For example,

Example

const myMap = new Map([
   ["key1", "value1"],
   ["key2", "value2"]
]);

console.log(myMap)
myMap.clear();
console.log(myMap)

Output

This will give the output −

Map { 'key1' => 'value1', 'key2' => 'value2' }
Map {}

Monica Mona
Monica Mona

Student of life, and a lifelong learner

Updated on: 15-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements