Skip to content

Instantly share code, notes, and snippets.

@funador
Last active August 2, 2018 21:22
Show Gist options
  • Save funador/cc59f925e43c0f8f1fee23f2780c47cb to your computer and use it in GitHub Desktop.
Save funador/cc59f925e43c0f8f1fee23f2780c47cb to your computer and use it in GitHub Desktop.
const dogs = [
{name: 'Snookums', needsShots: true, breed: 'pug', age: 2},
{name: 'Thor', needsShots: true, breed: 'hound', age: 1},
{name: 'Wunderbar', needsShots: false, breed: 'pug', age: 9},
]
// Lets see how a filter method might work...
const dogsToSeeVet = dogs.myFilter(function(dog) {
// Does the dog need a shot?
if(dog.needsShots === true) {
// Lets line them up with the doctor
return true
}
else {
// No need for this dog to see the vet
return false
}
})
// Of course that works! But we can stream line this a bit
const dogsToSeeVet2 = dogs.myFilter(dog => {
// return the value at the needsShots key which is either true or false
return dog.needsShots
})
// And for those flying fast these days, ES6 with an implied return
const dogsToSeeVet3 = dogs.myFilter(dog => dog.needsShots)
console.log(dogsToSeeVet) // Looks like Snookums and Thor
console.log(dogsToSeeVet2) // have a date with the Vet
console.log(dogsToSeeVet3) // to get their shots!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment