
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to remove all the elements from a map in JavaScript?
The map is basically a collection of elements where each element is stored as a Key, value pair. It can hold both objects and primitive values as either a key or a value. When we iterate over the map object it returns the key,value pair in the same order as inserted. The map has provided a method called map.clear() to remove the values inside a map. This method will remove every key/value pair and make the map totally empty.
syntax
map.clear(obj);
map.obj() takes an object as a parameter and removes each and every value so as to make it empty.
Example-1
In the following example, a map is created and 2 elements were passed to it. Before applying map.clear() method the size of the map object was two but after applying the size was zero.
<html> <body> <script> var myMap = new Map(); myMap.set(0, 'Tutorialspoint'); myMap.set(1, 'Tutorix'); document.write(myMap.size); document.write("</br>"); myMap.clear(); document.write(myMap.size); </script> </body> </html>
Output
2 0
Example-2
In the following example, a map is created and 4 elements were passed to it. Before applying map.clear() method the size of the map object was four but after applying the size was zero.
<html> <body> <script> var myMap = new Map(); myMap.set(0, 'India'); myMap.set(2, 'Australia'); myMap.set(3, 'England'); myMap.set(4, 'Newzealand'); document.write(myMap.size); document.write("</br>"); myMap.clear(); document.write(myMap.size); </script> </body> </html>
Output
4 0
- Related Articles
- How to remove all the elements from a set in javascript?
- How to remove all elements from a Collection in another Collection
- Remove all elements from a HashSet in Java
- Remove all elements from a HashSet in C#
- Remove all elements from a SortedList in C#
- Remove all elements from the ArrayList in Java
- Remove all elements from the ArrayList in C#
- Remove all elements from the SortedSet in C#
- Remove all elements from the Collection in C#
- Remove all elements from the Hashtable in C#
- Building frequency map of all the elements in an array JavaScript
- How to remove blank (undefined) elements from JavaScript array - JavaScript
- Remove all elements from TreeSet in Java
- Remove all elements from OrderedDictionary in C#
- How to remove duplicate elements from an array in JavaScript?
