Created
August 20, 2021 03:07
-
-
Save natafaye/6777ba94121adca28c235d0dabd23b67 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
let animals = [ | |
{ | |
type: "cat", | |
name: "Floof", | |
votes: 0 | |
}, | |
{ | |
type: "dog", | |
name: "Spot", | |
votes: 0 | |
}, | |
{ | |
type: "cat", | |
name: "Max", | |
votes: 0 | |
}, | |
{ | |
type: "elephant", | |
name: "Elle", | |
votes: 0 | |
} | |
] | |
let emails = [ | |
{ | |
from: "fdsfds", | |
unread: true | |
}, | |
{ | |
from: "fdsfds", | |
unread: false, | |
}, | |
]; | |
const unread = emails.filter(e => e.unread); | |
// If the filter method ceased to exist, we could make our own | |
function myFilter(array, callback) { | |
const filteredArray = []; | |
// We could do this with a normal for loop instead | |
// for(let i = 0; i < array.length; i++) { | |
// let item = array[i] | |
// const shouldKeep = callback(item) // either true or false | |
// if(shouldKeep) { | |
// filteredArray.push(item); | |
// } | |
// } | |
for(let item of array) { | |
const shouldKeep = callback(item) // either true or false | |
if(shouldKeep) { | |
filteredArray.push(item); | |
} | |
} | |
return filteredArray; | |
} | |
const unread = myFilter(emails, e => e.unread); |
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
function makeArrayString(array, callback) { | |
let string = ""; | |
for(let item of array) { | |
string += callback(item) + "\n"; | |
} | |
return string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment