ES6 Set - iteration
iterating over the elements of the set in ES6 set
by Varun Sukheja
JavaScript
const mySet = new Set([ 1, 5, 'some text', { a: 1, b: 2 } ]);
console.log('***** Iterating using for of *****');
for(let element of mySet){
console.log(element);
}
console.log('***** Iterating using forEach *****');
mySet.forEach(element => console.log(element));
console.log('***** Iterating uing Array Conversion *****');
for(let element of [...mySet]){
console.log(element);
}
console.log('***** Iterating uing keys() method *****');
for (let element of mySet.keys()) {
console.log(element);
}
console.log('***** Iterating uing values() method *****');
for (let element of mySet.values()) {
console.log(element);
}
console.log('***** Iterating uing entries() method *****');
for(let [key,value] of mySet.entries()){
console.log('key=',key,'| value=',value);
}