Safe code for new Set()
may look like:
let items = [];
for (let item of set)
if (isBad(item))
items.push(item);
for (let item of items)
set.delete(item)
Can I simplify code to:
for (let item of set)
if (isBad(item))
set.delete(item);
Safe code for new Map()
may look like:
let keys = [];
for (let [key, val] of map)
if (isBadKey(key) || isBadValue(val))
keys.push(key);
for (let key of keys)
map.delete(key)
Can I simplify code to:
for (let [key, val] of map)
if (isBadKey(key) || isBadValue(val))
map.delete(key)
util. HashMap. remove() is an inbuilt method of HashMap class and is used to remove the mapping of any particular key from the map. It basically removes the values for any particular key in the Map.
A Map is an iterable, so it can be directly iterated. Object does not implement an iteration protocol, and so objects are not directly iterable using the JavaScript for...of statement (by default).
map() method creates a new array with the results of calling a provided function on every element in this array.
The @@iterator method of a Map object implements the iterable protocol and allows maps to be consumed by most syntaxes expecting iterables, such as the spread syntax and for...of loops. It returns an iterator that yields the key-value pairs of the map.
Yes, you can simplify to that, it's totally safe.
From that last point follows that the only dangerous thing to do would be something like
const s = new Set([1]);
for (let x of s) {
s.delete(x);
s.add(1);
}
but not because of undefined behaviour or memory accumulation, but because of the infinite loop.
I would say yes, it's safe. When you iterate over the Set/Map using for ... of
under the hood the loop is going through @@iterator. And Iterator operates with .next()
only: so no indices and no matter what is before the current position. Only one next element is important.
So until you remove elements "in front of" the current iterator position - it's safe to do it.
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