Last active
January 20, 2022 21:01
-
-
Save tekul/77a51be05ac498b1fef6ba80a8d2cfe1 to your computer and use it in GitHub Desktop.
CYF filter and forEach discussion
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
// We start with an array of pet objects | |
let pets = [ | |
{ | |
name: "Hector", | |
type: "dog", | |
likes: "chicken" | |
}, | |
{ | |
name: "Mackie", | |
type: "cat", | |
likes: "fish" | |
}, | |
{ | |
name: "Ramona", | |
type: "cat", | |
likes: "cheese" | |
}, | |
{ | |
name: "Charlie", | |
type: "dog", | |
likes: "catfood" | |
}, | |
]; | |
console.log("All pets:"); | |
console.log(pets); | |
// Each element of the array is a pet object, so the element pets[0] | |
// is the dog called Hector. | |
console.log("Hector:"); | |
console.log(pets[0]); | |
// We can use the map method as before to convert the array into an | |
// array of just the pet names, by passing a function to map which | |
// returns the name of each pet. | |
function petName(pet) { | |
return pet.name; | |
} | |
let petNames = pets.map(petName); | |
// The new array is an array of strings (just the names) instead of objects | |
// As always when using map, it has the same length as the original array. | |
console.log(petNames); | |
// We can use filter to create a new array, which only contains the pets who | |
// are cats (based on the "type" property of each object). | |
// The function passed to filter should return true if we want to keep the | |
// pet in the new array, or false to leave it out (i.e. ignore it). | |
function isCat(pet) { | |
return pet.type === "cat"; | |
} | |
let cats = pets.filter(isCat); | |
console.log("Just the cats:"); | |
console.log(cats); | |
// We can filter based on other properties too | |
function likesFish(pet) { | |
return (pet.likes === "fish"); | |
} | |
// We can chain calls together. Here we filter first to get an array of cats | |
// (still an array of pet objects) then filter that again to get the cats who like fish. | |
cats = pets.filter(isCat).filter(likesFish); | |
console.log("Cats that like fish:"); | |
console.log(cats); | |
// forEach is different in that it just takes a function which does something for | |
// each element in the array. It doesn't return a new array and the function doesn't | |
// need to return anything either. | |
function sayHello(pet) { | |
if(pet.type === "cat") { | |
console.log("Meow!"); | |
} else { | |
console.log("Woof!"); | |
} | |
} | |
console.log("Each pet says Hello:"); | |
pets.forEach(sayHello); | |
// More advanced stuff below | |
// ========================= | |
// As with map, we can implement our own version of filter as a simple function | |
// which takes a function "keepElement" and an array as arguments | |
// As with map it creates a new array to store the elements we want to keep, | |
// it iterates through the provided array and adds the elements for which the | |
// function returns true.to the new array | |
function myFilter(keepElement, arr) { | |
let newArray = []; | |
for(i=0; i < arr.length; i++) { | |
if(keepElement(arr[i])) { | |
newArray.push(arr[i]); | |
} | |
} | |
return newArray; | |
} | |
// We can then check that we get the same result when filtering for cats. | |
// So for our keepElement function we pass the function isCat which we | |
// used above with filter. | |
cats = myFilter(isCat, pets); | |
console.log("Filter for cats again using our implementation:"); | |
console.log(cats); | |
// Implementing forEach is simple by comparison | |
// It just calls the function with every element in the array | |
function myForEach(f, arr) { | |
for(i=0; i < arr.length; i++) { | |
f(arr[i]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment