Created
December 15, 2015 21:01
-
-
Save marcveens/0a99fe33843f8514c232 to your computer and use it in GitHub Desktop.
Use .map instead of a for loop
This file contains hidden or 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
| // WRONG | |
| var mixedEmails = ['JOHN@ACME.COM', 'Mary@FooBar.com', 'monty@spam.eggs']; | |
| var tempList = []; | |
| for (var i = 0; i < mixedEmails.length; i++) { | |
| tempList.push(mixedEmails[i].toLowerCase()); | |
| } | |
| document.getElementsByTagName('body')[0].innerHTML += 'Old method: ' + tempList; | |
| // RIGHT | |
| var mixedEmails = ['JOHN@ACME.COM', 'Mary@FooBar.com', 'monty@spam.eggs']; | |
| var validEmails = []; | |
| function toLower(str) { | |
| return str.toLowerCase(); | |
| } | |
| validEmails = mixedEmails.map(toLower); | |
| document.getElementsByTagName('body')[0].innerHTML += '<br><br>Right method: ' + validEmails |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment