Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save astfarias/239169c7c41b467708530a303d6b6d0f to your computer and use it in GitHub Desktop.
Save astfarias/239169c7c41b467708530a303d6b6d0f to your computer and use it in GitHub Desktop.
JS - Functional Programming - Higher-order Functions - map()
// 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