Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

map.delete(key) during map.forEach

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).

like image 492
Lonnie Best Avatar asked Sep 11 '17 05:09

Lonnie Best


People also ask

How to remove a key from map js?

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.

How do I delete a map key?

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.

How do I delete an object in maps?

delete() The delete() method removes the specified element from a Map object by key.

How do you delete a map in node JS?

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.

How to remove/delete a specific key key from a map Map?

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.

How do I use foreach() method in map?

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) => { /* …

What happens if key is not present in the 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.

When foreach is invoked for a deleted key in JavaScript?

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.


1 Answers

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.

like image 72
Jaffer Wilson Avatar answered Oct 18 '22 21:10

Jaffer Wilson