-
-
Save roshanca/cd3837c78a446c3960a8ee64393f0031 to your computer and use it in GitHub Desktop.
map vs. flatMap
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
const map = (array, func) => ( | |
array.map(func) | |
); | |
const flatMap = (array, func) => ( | |
array.reduce((result, element) => ( | |
result.concat(func(element)) | |
), []) | |
); | |
const arr = ['two birds', 'three green peas']; | |
const split = s => s.split(' '); | |
map(arr, split) | |
// [["two", "birds"], ["three", "green", "peas"]] | |
flatMap(arr, split) | |
// ["two", "birds", "three", "green", "peas"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment