Skip to content

Instantly share code, notes, and snippets.

@rogerwelin
Created December 7, 2017 18:01
Show Gist options
  • Save rogerwelin/eda5e161acf3db8e8eb425d5fc84ddf0 to your computer and use it in GitHub Desktop.
Save rogerwelin/eda5e161acf3db8e8eb425d5fc84ddf0 to your computer and use it in GitHub Desktop.
fun-fun-function-map-2
// we want to filter out all names
var animals = [
{ name: 'fluffykins', species: 'rabbit' },
{ name: 'caro', species: 'dog' },
{ name: 'jimmy', species: 'fish' },
{ name: 'ursula', species: 'cat' },
{ name: 'hamilton', species: 'dog' }
]
// functional way using map - but we can also retun a modified object
// using + ' is a' + animal.species for example
var names = animals.map(function(animal) {
return animal.name
})
// there is a shorter syntax called arrow functions
var names2 = animals.map((animal) => animal.name)
console.log(names2)
/*
var names = []
for (var i = 0; i < animals.length; i++) {
names.push(animals[i].name)
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment