Typically, you cannot safely delete items from an list while you're looping through that list. Does this concept remain true for ES6 Maps?
I tried this simple test without exceptions:
var map = new Map([['a',1],['b',2],['c',3]]);
map.forEach((value,key,map)=>
{
map.delete(key);
let str = `["${key}",${value}] was deleted. `;
str += `map.size = ${map.size}`;
console.log(str);
});
It seems ok.
Update: I just read this reference from Mozilla. It is certainly doable. I'd be interested in any performance benchmarks comparing this method of deletion with other methods (on larger datasets).
Map.delete() Method in JavaScript The Map. delete() method takes the key which needs to be removed from the map, thus removes the element associated with that key and returns true. If the key is not present then it returns false.
To delete a key from a map, we can use Go's built-in delete function. It should be noted that when we delete a key from a map, its value will also be deleted as the key-value pair is like a single entity when it comes to maps in Go.
delete() The delete() method removes the specified element from a Map object by key.
Map.clear() Method in JavaScript clear() method in JavaScript is used for the removal of all the elements from a map and make it empty. It removes all the [key, value] from the map. No arguments are required to be sent as parameters to the Map. clear() method and it returns an undefined return value.
The syntax to remove/delete a specific key key from a Map map using delete () method is In the following example, we take a Map with keys 'a', 'b', and 'c'. We shall delete the key 'b' using delete () method. If the specified key is not present, then delete () method does nothing and returns false.
The forEach () method executes a provided function once per each key/value pair in the Map object, in insertion order. forEach(() => { /* … */ } ) forEach((value) => { /* … */ } ) forEach((value, key) => { /* … */ } ) forEach((value, key, map) => { /* …
If the key passed as an argument to the function is not present in the map then it returns false. Basically, it neither throws any exception nor does it have any error. Map.clear () removes all key-value pairs of the map and reduces the size of the map to zero.
It is not invoked for keys which have been deleted. However, it is executed for values which are present but have the value undefined . If a thisArg parameter is provided to forEach, it will be passed to callback when invoked, for use as its this value. Otherwise, the value undefined will be passed for use as its this value.
Well I guess you ar eright. I am not quite familiar with the ES6 Maps, but had done a bit of research and found this blog a bit helpful where it explains about the MAPS:
https://hackernoon.com/what-you-should-know-about-es6-maps-dc66af6b9a1e
Here you will get the deleting mechanism explanation too:
Something like this:
var m = new Map()
m.set('a', 1)
m.set('b', 2)
m.delete('a'); // true
m.delete('c'); // false (key was not there to delete)
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With