In order to iterate a map in Typescript.
The forEach
won't be interrupted. Strangely the key and value have to be inverted.
The for
works correctly.
const map = new Map<number, number>()
const uniq = new Set<number>();
// won't return anything if condition is true
map.forEach( (v,k) => { // moreover , k and v are inverted
if(uniq.has(v)) return false
uniq.add(v)
});
// will work
for (const [_, v] of map.entries()) {
if(uniq.has(v)) return false
uniq.add(v)
}
Why forEach
doesn't break or return ?