A map, also known as a dictionary, is a data structure to store key-value pairs. It consists of keys with their corresponding values. With the delete()
method, we can delete a key-pair entry by just specifying the key.
Set.delete(key)
key
: This is the key whose value we want to delete.This method returns a boolean value. If the key is found and the entry is deleted, it returns true
. Otherwise, it returns false
.
// Create some mapslet evenNumbers = new Map<string, number>([["two", 2], ["four", 4], ["eight", 8]])let cart = new Map<string, number>([["rice", 500], ["bag", 10]])let countries = new Map<string, string>([["NG", "Nigeria"], ["BR", "Brazil"], ["IN", "India"]])let isMarried = new Map<string, boolean>([["James", false], ["Jane", true], ["Doe", false]])let emptyMap = new Map<null, null>()// Delete some entriesconsole.log(evenNumbers.delete("two")) // trueconsole.log(cart.delete("bag")) // trueconsole.log(countries.delete("GH")) // falseconsole.log(isMarried.delete("Doe")) // trueconsole.log(evenNumbers.delete(null)) // false// Log out modified mapsconsole.log(evenNumbers)console.log(cart)console.log(countries)console.log(isMarried)console.log(emptyMap)
delete()
method, we delete some entries by specifying their keys.