
- 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
Remove elements from a Dictionary using Javascript
To remove an element from the dictionary, we first need to check if it exists in the dictionary.
We'll use the hasKey method for that. Then we can directly delete it using the delete operator.
We'll return a Boolean so that the place where we call this method can know whether the key already existed or not in the dictionary.
Example
delete(key) { if(this.hasKey(key)) { delete this.container[key]; return true; } return false; }
You can test this using −
Example
const myMap = new MyMap(); myMap.put("key1", "value1"); myMap.put("key2", "value2"); myMap.display(); myMap.delete("key2"); myMap.display();
Output
This will give the output −
{ key1: 'value1', key2: 'value2' } { key1: 'value1' }
In ES6, you have the delete method to remove values from the map. For example,
Example
const myMap = new Map([ ["key1", "value1"], ["key2", "value2"] ]); myMap.delete("key2"); console.log(myMap.has("key1")) console.log(myMap.has("key2"))
Output
This will give the output −
True False
- Related Articles
- Remove elements from a queue using Javascript
- Remove elements from a PriorityQueue using Javascript
- Remove elements from a Set using Javascript
- Remove elements from a linked list using Javascript
- Remove elements from array using JavaScript filter - JavaScript
- Python Program to remove duplicate elements from a dictionary
- Remove elements from array in JavaScript using includes() and splice()?
- Remove elements from Javascript Hash Table
- Remove elements from singly linked list in JavaScript
- Add elements to a Dictionary in Javascript
- Python - Ways to remove a key from dictionary
- How to remove blank (undefined) elements from JavaScript array - JavaScript
- How to remove a key from a python dictionary?
- Clearing a Dictionary using Javascript
- Peeking elements from a PriorityQueue using JavaScript

Advertisements