O método forEach é utilizado para percorrermos todos os elementos de um Set.
Uma função(callback), função anônima ou função arrow, é passada
como argumento e será executada para todo os elementos.
Sintaxe 1
forEach(() => { /* … */ } )
forEach((value) => { /* … */ } )
forEach((value, key) => { /* … */ } )
forEach((value, key, set) => { /* … */ } )
Sintaxe 2
forEach(callbackFn)
forEach(callbackFn, thisArg)
Sintaxe 3
forEach(function() { /* … */ })
forEach(function(value) { /* … */ })
forEach(function(value, key) { /* … */ })
forEach(function(value, key, set) { /* … */ })
forEach(function(value, key, set) { /* … */ }, thisArg)
Onde
value: valor do elemento atual
key: chave do elemento atual
set: objeto Set iterado
thisArg: argumento this passado para função
callbackFn: função passada como argumento
Exemplo 5
let mset = new Set("string");
mset.forEach((value) =>{
console.log(value);
});
Saída
s
t
r
i
n
g