Created
April 3, 2023 11:32
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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