console.log('demo1 - show an iterator and first item in list');
let anEmptySet = new Set();
let aSet = new Set(['item1', 'item2', 'item3']);
demo1('Showing an EmptySet', anEmptySet);
demo1('aSet', aSet);
console.log('demo2 - Easiest way to loop over a collection (using iterator) (for of)');
demo2('anEmptySet', anEmptySet);
demo2('aSet', aSet);
console.log('demo3 - iterator using for() loop');
demo3('anEmptySet', anEmptySet)
demo3('aSet', aSet)
demo4('demo4 - iterator using while loop')
demo4('anEmptySet', anEmptySet)
demo4('aSet', aSet)
/* Show how to get an iterator */
function demo1(aMsg, aCollection){
console.log('demo1: ' + aMsg);
let it = aCollection[Symbol.iterator]();
console.log('iterator is', it)
console.log('it.done - is not meaningful', it.done); // notice that it.done is not defined!
let itLoop = it.next() // it
console.log('it.next()', itLoop);
console.log('it.next().value', itLoop.value);
console.log('it.next().done', itLoop.done);
console.log('\n\n');
}
function demo2(aMsg, aCollection){
console.log('demo2: ' + aMsg);
for(let item of aCollection){
console.log('for of returns ', item);
}
console.log('\n\n');
}
function demo3(aMsg, aCollection){
console.log('demo3: ' + aMsg);
for(let it = aSet[Symbol.iterator](), loop = it.next(); ! loop.done; loop = it.next()){
console.log('for with index ', loop.value);
}
console.log('\n\n');
}
function demo4(aMsg, aCollection){
console.log('demo4: ' + aMsg);
let it = aSet[Symbol.iterator]();
let loop = it.next();
while (! loop.done){
console.log('for with index ', loop.value);
loop = it.next();
}
console.log('\n\n');
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.