The delete
method of the map object is used to remove an entry associated with a passed key.
mapObj.delete(key);
key
: the key of the entry to be deleted from the map.
The delete
method returns true
if there is an entry for the passed key and returns false
otherwise.
let map = new Map();map.set(1, "one");map.set(2, "two");console.log("The map is ", map);console.log("Removing the key 1 : ", map.delete(1));console.log("The map is ", map);console.log("Removing the key 3 : ", map.delete(3));console.log("The map is ", map);
In the code above:
We created a map object with two entries.
We then called the delete
method for the key 1
. The key 1
is present. So the entry is removed from the map and true
is returned.
We called the delete
method for the key 3
. The key 3
is not present so false
is returned.