const mySet = new Set( [ { a: 1 }, { b: 2 } ] );
// A) Using forEach()
mySet.forEach( item => console.log( item ) );
// B) Using for ... of
for (let item of mySet) {
console.log(item);
}
// Output:
// { a: 1 }
// { b: 2 }
const set = new Set([1, 2, 3]);
// looping through Set
for (let i of set) {
console.log(i);
}
function logSetElements(value1, value2, set) {
console.log(`s[${value1}] = ${value2}`);
}
new Set(['foo', 'bar', undefined]).forEach(logSetElements);
// expected output: "s[foo] = foo"
// expected output: "s[bar] = bar"
// expected output: "s[undefined] = undefined"