Skip to content

Instantly share code, notes, and snippets.

@veganstraightedge
Last active May 2, 2017 22:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veganstraightedge/92d7753479c4232f97ca1c5daa723247 to your computer and use it in GitHub Desktop.
Save veganstraightedge/92d7753479c4232f97ca1c5daa723247 to your computer and use it in GitHub Desktop.
For when you can't remember the difference between .each and .map, here's a reminder! It's written in Ruby, but the principle is the same in Javascript.
# In Ruby
# Array.each
# Iterate through each a collection unchanged
User.all.each do |user|
puts user.email
puts user.name
puts
end
# => veganstraightedge@gmail.com
# => Shane Becker
# =>
# => aki@example.com
# => Aki Braun
# =>
# Array.map
# Create a new collection by manipulating an original collection
# Optionally, iterate over the new one
emails = User.all.map{ |u| u.email }
puts emails
# => ["veganstraightedge@gmail.com", "aki@example.com"]
emails.each do |email|
puts email
end
# => veganstraightedge@gmail.com
# => aki@example.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment