Created
August 15, 2017 06:08
-
-
Save astfarias/239169c7c41b467708530a303d6b6d0f to your computer and use it in GitHub Desktop.
JS - Functional Programming - Higher-order Functions - map()
This file contains hidden or 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
// Example of map as a higher-order function because uses a callback function | |
var animals = [ | |
{ name: 'Fluffykins', species: 'rabbit' }, | |
{ name: 'Caro', species: 'dog' }, | |
{ name: 'Hamilton', species: 'dog' }, | |
{ name: 'Harold', species: 'fish' }, | |
{ name: 'Ursula', species: 'cat' }, | |
]; | |
// map all animals and his species | |
var names = animals.map(function(animal){ | |
return animal.name + ' is a ' + animal.species | |
}) | |
/** | |
names [ 'Fluffykins is a rabbit', | |
'Caro is a dog', | |
'Hamilton is a dog', | |
'Harold is a fish', | |
'Ursula is a cat' ] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment