The clear()
method of a map in TypeScript clears all its key-value entries.
Map.clear()
map
: This is the map on which we apply the clear()
function to clear its key-value pairs.This method returns an empty map.
// 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>()// Log out the maps before clearing their entriesconsole.log("Maps before clearing their entries")console.log(evenNumbers)console.log(cart)console.log(countries)console.log(isMarried)console.log(emptyMap)// Clear the mapsevenNumbers.clear()cart.clear()countries.clear()isMarried.clear()emptyMap.clear()// Log out the maps after clearing their entriesconsole.log("\nMaps after clearing their entries")console.log(evenNumbers)console.log(cart)console.log(countries)console.log(isMarried)console.log(emptyMap)
clear()
method.