Created
April 6, 2017 00:32
-
-
Save marcoslhc/a00b7357ead56af303aac42328da9123 to your computer and use it in GitHub Desktop.
array reduce concat
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
| var arr = ['The', 'simple', 'things', 'of', 'life'] | |
| arr.reduce(function (finalString, str) { | |
| return finalString.concat(' ').concat(str) | |
| }, ''); | |
| // " The simple things of life" | |
| // reduce folds the value from left to righ, we could also reduce from right to left: | |
| var arr = 'stressed'.split(''); | |
| arr.reduceRight(function (finalString, str) { | |
| return finalString.concat(str); | |
| }, ''); | |
| // "desserts" The reversed string proves that the right most element was processed first and so on |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment