Skip to content

Instantly share code, notes, and snippets.

@prashantsani
Created April 3, 2023 11:32
Show Gist options
  • Save prashantsani/bee018060da7cd96919a9faa7866384b to your computer and use it in GitHub Desktop.
Save prashantsani/bee018060da7cd96919a9faa7866384b to your computer and use it in GitHub Desktop.
Using Symbol.iterator to update how (for of) loop iterates through an object
const testingTeam = {
lead: 'Alex',
engineer: 'Bill',
// This is generator function
[Symbol.iterator]: function* () {
yield this.lead;
yield this.engineer
}
}
const engineeringTeam = {
testingTeam,
lead: 'Prashant',
engineer: 'Sudarshan',
manager: 'Harshit',
// This is generator function
[Symbol.iterator]: function* () {
yield this.lead;
yield this.engineer;
yield this.manager;
yield* this.testingTeam;
}
}
let allEngineers = []
for(teamMember of engineeringTeam) {
allEngineers.push(teamMember);
}
console.log(allEngineers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment