Skip to content

Instantly share code, notes, and snippets.

@rogerwelin
Created December 7, 2017 17:59
Show Gist options
  • Save rogerwelin/c17eba179643cf3532ab5c99cd7db233 to your computer and use it in GitHub Desktop.
Save rogerwelin/c17eba179643cf3532ab5c99cd7db233 to your computer and use it in GitHub Desktop.
fun-fun-function-filter-1
// we want to filter out all dogs
var animals = [
{ name: 'fluffykins', species: 'rabbit' },
{ name: 'caro', species: 'dog' },
{ name: 'jimmy', species: 'fish' },
{ name: 'ursula', species: 'cat' },
{ name: 'hamilton', species: 'dog' }
]
// functional way - compare with example below
var dogs = animals.filter(function(animal) {
return animal.species === 'dog'
})
// another way
var isDog = function(animal) {
return animal.species === 'dog'
}
var dogs = animals.filter(isDog)
var otherAnimals = animals._.reject(isDog)
console.log(dogs)
/*
var dogs = []
for (var i = 0; i < animals.length; i++) {
if (animals.species === 'dog') {
dogs.push(animals[i])
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment