Clearing the set using Javascript

Web DevelopmentJavascriptFront End Technology

The clear method is pretty straightforward. We can just reassign the container variable to a new object and the set will now be empty. This can be implemented as follows − 

Example

clear() {
   this.container = {};
}

You can test this using −

Example

const testSet = new MySet();

testSet.add(1);
testSet.add(2);
testSet.add(5);

testSet.display();
testSet.clear();
testSet.display();

Output

This will give the output −

{ '1': 1, '2': 2, '5': 5 }
{ }
raja
Updated on 15-Jun-2020 09:39:55

Advertisements