Skip to content

Instantly share code, notes, and snippets.

@mpj
Last active May 25, 2023 15:48
Show Gist options
  • Save mpj/c5ae804e576042b3287d to your computer and use it in GitHub Desktop.
Save mpj/c5ae804e576042b3287d to your computer and use it in GitHub Desktop.
Code to the video - "Map: Part 2 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 names = []
for (var i = 0; i < animals.length; i++) {
names.push(animals[i].name)
}
var names = animals.map(function(animal) { return animal.name })
var names = animals.map(function(animal) { animal.name + ' the ' + animal.species })
var names = animals.map((animal) => animal.name)
var names = animals.map((x) => x.name)
@rick4470
Copy link

rick4470 commented Aug 5, 2015

0-array.js should have a comma on line 5

@PeterNMcArthur
Copy link

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' } ]

You're missing a comma after the Harold object. :)

Thanks for your videos I'm really enjoying your series on functional programming.

@curiouslychase
Copy link

Also, the name prop in the for is backwards animals.name[i] should be animals[i].name.

@mpj
Copy link
Author

mpj commented Aug 23, 2015

Thanks for pointing that out! Fixed!

@Hacking-NASSA-with-HTML
Copy link

Hacking-NASSA-with-HTML commented Dec 23, 2020

@mpj , Thanks for the simple and great tutorial 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment