Saturday 20 October 2018

JavaScript: Set.prototype.delete(value): Remove element from set

Set.prototype.delete(value)
‘delete’ method removes the element from set. This method return true, if the element is removed, else false.

HelloWorld.js
function printSetInfo(set){
  console.log("***********************************");
  console.log("Number Of Elements : " + set.size);
  
  for(var key of countries){
    console.log(key);
  }
  console.log("***********************************");
}

var countries =  new Set();

countries.add("India");
countries.add("Australia");
countries.add("Canada");
countries.add("Germany");

printSetInfo(countries);

var removed = countries.delete("Australia");
console.log("Is Australia removed ? " + removed);

removed = countries.delete("France");
console.log("Is France removed ? " + removed);

printSetInfo(countries);


Output

***********************************
Number Of Elements : 4
India
Australia
Canada
Germany
***********************************
Is Australia removed ? true
Is France removed ? false
***********************************
Number Of Elements : 3
India
Canada
Germany
***********************************



Previous                                                 Next                                                 Home

No comments:

Post a Comment