Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Fun Fun Function
- * Higher-order functions - Part 1 of Functional Programming in JavaScript
- */
- var animals = [
- { name: 'Fluffykins', species: 'rabbit' },
- { name: 'Caro', species: 'dog' },
- { name: 'Hamilton', species: 'dog' },
- { name: 'Harold', species: 'fish' },
- { name: 'Ursula', species: 'cat' },
- { name: 'Jimmy', species: 'fish' },
- ];
- var dogs = [];
- for (var i = 0; i < animals.length; i++) {
- if (animals[i].species === 'dog') {
- dogs.push(animals[i])
- }
- }
- console.log(dogs)
- var isDog = function(animal) {
- return animal.species === 'dog'
- }
- var dogs2 = animals.filter(isDog);
- console.log(dogs2);
Add Comment
Please, Sign In to add comment