Created
August 22, 2020 12:23
-
-
Save jindeveloper/4fd023b4bcef57ba5f5b83b35b583b21 to your computer and use it in GitHub Desktop.
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
/*forEach loop*/ | |
let amounts = [1.25, 2.25, 3.25, 4.25]; | |
/** | |
* Outputs: | |
* 1.25 | |
* 2.25 | |
* 3.25 | |
* 4.25 | |
*/ | |
amounts.forEach((item) => { | |
console.log(item); | |
}); | |
/*end of forEach loop*/ | |
/** Start of for-of loop */ | |
let customers = { firstName: "Jin Vincent", lastName: "Necesario", country: "Philippines" }; | |
for (const customer of customers) { | |
//TypeError: customers is not iterable | |
} | |
/** End of for-of loop */ | |
/** Start of for-in loop */ | |
let customer = { firstName: "Jin Vincent", lastName: "Necesario", country: "Philippines" }; | |
/** | |
* Output: | |
* firstName | |
* lastName | |
* country | |
*/ | |
for (let key in customer) { | |
console.log(key); | |
} | |
/** End of for-in loop */ | |
/** Start of for-in loop that iterates over an Array */ | |
/** Warning: Please don't do this on your project/product! */ | |
Array.prototype.bar = 1; | |
let products = [1001, "mouse", "monitor", { firstName: "Jin Vincent" }]; | |
/**Output: | |
* 0 | |
* 1 | |
* 2 | |
* 3 | |
* bar | |
*/ | |
for (let prop in products) { | |
console.log(prop); //this outputs the index of the array and bar | |
} | |
/** End of for-in loop that iterates over an Array*/ | |
//declare a JavaScript string type | |
let programmingLanguage = "JavaScript"; | |
//lets implement the @@iterator property key | |
let iterator = programmingLanguage[Symbol.iterator](); | |
//let's check if the @@iterator property function return an iterator | |
console.log(iterator); //output: StringIterator | |
/** Start of how to check if a type/object implements the Symbol.iterator */ | |
let programmingLanguage = "JavaScript"; | |
//let's us check if the built-in data-type String implements Symbol.iterator | |
Object.getOwnPropertySymbols(programmingLanguage.__proto__); | |
/** end of how to check if type/object implements the Symbol.iterator */ | |
/** Start of for of loop */ | |
const xmenGoldTeam = ['Storm', 'Bishop', 'Colossus', 'Jean Grey', 'Iceman', 'Archangel']; | |
/**Outputs: | |
* Storm | |
* Bishop | |
* Colossus | |
* Jean Grey | |
* Iceman | |
* Archangel | |
*/ | |
for (let xmen of xmenGoldTeam) { | |
console.log(xmen); | |
} | |
/** end of for of loop */ | |
/*Start of an iterator protocol*/ | |
const countries = { | |
collection: ["Philippines", "Singapore", "Malaysia", "Canada", "Brazil", "Australia"], | |
index: 0, | |
next: function () { | |
if (this.index < this.collection.length) { | |
return { | |
value: this.collection[this.index++], | |
done: false | |
} | |
} else { | |
return { | |
done: true | |
} | |
} | |
} | |
} | |
console.log(countries.next()); //output: {value: "Philippines", done: false} | |
console.log(countries.next()); //output: {value: "Singapore", done: false} | |
console.log(countries.next()); //output: {value: "Malaysia", done: false} | |
console.log(countries.next()); //output: {value: "Canada", done: false} | |
console.log(countries.next()); //output: {value: "Brazil", done: false} | |
console.log(countries.next()); //output: {value: "Australia", done: false} | |
console.log(countries.next()); //output: {done: true} | |
/*End of an iterator protocol*/ | |
/*Start of an iterable protocol*/ | |
const countries = { | |
countryCollection: ["Philippines", "Singapore", "Malaysia", "Canada", "Brazil", "Australia"], | |
startIndex: 0, | |
[Symbol.iterator]: function () { | |
return { | |
collection: this.countryCollection, | |
index: this.startIndex, | |
next: function () { | |
if (this.index < this.collection.length) { | |
return { | |
value: this.collection[this.index++], | |
done: false | |
} | |
} else { | |
return { | |
done: true | |
} | |
} | |
} | |
} | |
} | |
} | |
let countryIterator = countries[Symbol.iterator](); | |
console.log(countryIterator.next()); //output: {value: "Philippines", done: false} | |
console.log(countryIterator.next()); //output: {value: "Singapore", done: false} | |
console.log(countryIterator.next()); //output: {value: "Malaysia", done: false} | |
console.log(countryIterator.next()); //output: {value: "Canada", done: false} | |
console.log(countryIterator.next()); //output: {value: "Brazil", done: false} | |
console.log(countryIterator.next()); //output: {value: "Australia", done: false} | |
console.log(countryIterator.next()); //output: {done: true} | |
/** | |
* Outputs: | |
* Philippines | |
* Singapore | |
* Malaysia | |
* Canada | |
* Brazil | |
* Australia | |
* undefined | |
*/ | |
for (country of countries) { | |
console.log(country) | |
} | |
/*End of an iterable protocol*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment