Last active
May 2, 2017 22:41
-
-
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.
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
# 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