Created
December 7, 2017 17:59
-
-
Save rogerwelin/c17eba179643cf3532ab5c99cd7db233 to your computer and use it in GitHub Desktop.
fun-fun-function-filter-1
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 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