Skip to content

Instantly share code, notes, and snippets.

@natafaye
Created August 20, 2021 03:07
Show Gist options
  • Save natafaye/6777ba94121adca28c235d0dabd23b67 to your computer and use it in GitHub Desktop.
Save natafaye/6777ba94121adca28c235d0dabd23b67 to your computer and use it in GitHub Desktop.
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);
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