Skip to content

Instantly share code, notes, and snippets.

@natafaye
Created January 26, 2022 02:38
Show Gist options
  • Save natafaye/23e84ca0fca7bc839ba654b576e899d2 to your computer and use it in GitHub Desktop.
Save natafaye/23e84ca0fca7bc839ba654b576e899d2 to your computer and use it in GitHub Desktop.
// Normal function
function isNotEmpty(paper) {
return paper !== "";
}
// Arrow function
const isNotEmpty = (paper) => {
return paper !== "";
}
// Arrow function no parenthesis
const isNotEmpty = paper => {
return paper !== "";
}
// Arrow function no curly brackets or return
const isNotEmpty = paper => paper !== "";
// Arrow function shortened parameter name
const isNotEmpty = p => p !== "";
// We could use it with filter
const fullPapers = papers.filter( p => p !== "" )
// internFunction( bobCallbackFunction )
const emailList = [
{
id: 234,
author: {
firstName: "Natalie",
lastName: "Childs"
},
to: "Calvin",
message: "Heyyyy",
read: false,
tags: [
"not important",
"personal"
]
},
{
id: 1654,
author: {
firstName: "Natalie",
lastName: "Childs"
},
to: "Calvin",
message: "What's up?",
read: true,
tags: [
"work"
]
},
{
id: 474,
author: {
firstName: "Dylan",
lastName: "Green"
},
to: "Calvin",
message: "Good day",
read: false,
tags: []
}
]
// Display the Emails - Map
const displayEmails = emailList.map( email => email.author.firstName + ": " + email.message );
alert("Here are the emails:\n" + displayEmails.join("\n") );
// Mark All As Read - ForEach
// We could do it with a for loop
// for(let i = 0; i < emailList.length; i++) {
// emailList[i].read = true;
// }
// We could do it with for-of
// for( const email of emailList ) {
// email.read = true;
// }
// Experienced developers would probably use forEach in this situation
emailList.forEach( email => email.read = true );
// Find an Email - Find
const emailFromNatalie = emailList.find( email => email.author.firstName === "Natalie" )
// Really Common
const idOfEmailToDelete = 1654;
const emailToDelete = emailList.find( email => email.id === idOfEmailToDelete )
// deletes it
// Find All Emails From One Person - Filter
const allEmailsFromNatalie = emailList.filter( email => email.author.firstName === "Natalie" )
// All Unread
const allUnreadEmails = emailList.filter( email => !email.read )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment