Last active
December 23, 2021 02:47
-
-
Save natafaye/a20e88a3fc716288bd8f16502dcc70be to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Working with one email | |
const emailFromNatalie = { | |
author: "Natalie", | |
to: "Calvin", | |
message: "Heyyyy", | |
read: false | |
} | |
alert(emailFromNatalie.message) |
This file contains hidden or 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
// Working with an array of emails (this variable and the array inside is used in the later examples) | |
const emailList = [ | |
{ | |
id: 234, | |
author: "Natalie", | |
to: "Calvin", | |
message: "Heyyyy", | |
read: false | |
}, | |
{ | |
id: 1654, | |
author: "Xee", | |
to: "Calvin", | |
message: "What's up?", | |
read: true | |
}, | |
{ | |
id: 474, | |
author: "Dylan", | |
to: "Calvin", | |
message: "Good day", | |
read: false | |
} | |
] | |
alert( emailList[0].message ) |
This file contains hidden or 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
// Making a nice alert of the emails using a for loop | |
// !! Uses the emailList variable in array-of-emails.js !! | |
let emailListString = ""; | |
for(let i = 0; i < emailList.length; i++) { | |
emailListString += emailList[i].author + ": " + emailList[i].message + "\n"; | |
} | |
alert(emailListString) |
This file contains hidden or 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
// Making a nice alert of the emails using for each | |
// !! Uses the emailList variable in array-of-emails.js !! | |
let emailListString = ""; | |
function addToListString(email) { | |
emailListString += email.author + ": " + email.message + "\n"; | |
} | |
emailList.forEach(addToListString); |
This file contains hidden or 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
// The process of converting the normal function way into the more commonly used arrow function way | |
// !! Uses the emailList variable in array-of-emails.js !! | |
// Normal | |
function addToListString(email) { | |
emailListString += email.author + ": " + email.message + "\n"; | |
} | |
emailList.forEach(addToListString); | |
/* CAN BE CONVERTED TO */ | |
// Arrow | |
const addToListString = (email) => { | |
emailListString += email.author + ": " + email.message + "\n"; | |
} | |
emailList.forEach(addToListString); | |
/* CAN BE CONVERTED TO */ | |
// No curly brackets | |
const addToListString = (email) => emailListString += email.author + ": " + email.message + "\n"; | |
emailList.forEach(addToListString); | |
/* CAN BE CONVERTED TO */ | |
// No parenthesis | |
const addToListString = email => emailListString += email.author + ": " + email.message + "\n"; | |
emailList.forEach(addToListString); | |
/* CAN BE CONVERTED TO */ | |
// Passing in the function itself | |
// This is the way experienced developers tend to do it | |
emailList.forEach( email => emailListString += email.author + ": " + email.message + "\n" ); |
This file contains hidden or 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
// Making a nice alert of the emails using map | |
// !! Uses the emailList variable in array-of-emails.js !! | |
const emailStringList = emailList.map( e => e.author + ": " + e.message ); | |
alert( emailStringList.join("\n\n") ); |
This file contains hidden or 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
// Finds an email with a particular id (or you could find it by author or whatever) | |
// !! Uses the emailList variable in array-of-emails.js !! | |
const emailFromNatalie = emailList.find( email => email.id === 1654 ) | |
alert( emailFromNatalie.message ); |
This file contains hidden or 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
// Filters emails and gives only the unread ones, then marks them all as read, then filters for unread again | |
// !! Uses the emailList variable in array-of-emails.js !! | |
let unreadEmails = emailList.filter( e => !e.read ) | |
alert("You have " + unreadEmails.length + " unread emails"); | |
// Set all emails as read | |
emailList.forEach( email => email.read = true ); | |
unreadEmails = emailList.filter( e => !e.read ) | |
alert("You have " + unreadEmails.length + " unread emails"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment