Created
December 7, 2017 18:01
-
-
Save rogerwelin/eda5e161acf3db8e8eb425d5fc84ddf0 to your computer and use it in GitHub Desktop.
fun-fun-function-map-2
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 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