Skip to content

Instantly share code, notes, and snippets.

@veganstraightedge
Last active May 2, 2017 22:41
Show Gist options
  • 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
# => [email protected]
# => Shane Becker
# =>
# => [email protected]
# => 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
# => ["[email protected]", "[email protected]"]
emails.each do |email|
puts email
end
# => [email protected]
# => [email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment