map.delete(key) during map.forEach
Why? If you are working with the same instance, actually you can delete. It have functions which are used to delete an item, so you can delete.
But from the side of Optimization don't delete any item from the Map
or array
. Javascript engines have optimizations based on the shape of the object. If it is the same over some refers to that object, it would be optimized. Instead of this create a new object from the filtered values of the current object.
var map = new Map([['a',1],['b',2],['c',3]]);
map.forEach((value,key,map)=>
{
map.delete(key);
});
console.log(map);
There are some languages ( C# ), that you can't remove item from the IEnumerable
in the for each loop
, because it works another way under the hood, actually it gives you only read and update access, not delete.
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.