Clearing a Dictionary using Javascript

Web DevelopmentFront End TechnologyJavascript

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 {}
raja
Published on 27-Jun-2018 17:34:51
Advertisements