Last active
August 2, 2019 08:21
-
-
Save kutyel/73c0bf389086bf14d157d44af3a941c0 to your computer and use it in GitHub Desktop.
Array.prototype.flatMap
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
const array = [1, 3] | |
const f = x => [x, x + 1] | |
array.map(f) // > [[1, 2], [3, 4]] | |
array.flatMap(f) // > [1, 2, 3, 4] | |
// Ahora un ejemplo un poco más curioso... | |
const heroes = ["Eren Jaeger", "Mikasa Ackerman"] | |
const splitBySpace = xs => xs.split(" ") | |
heroes.map(splitBySpace) // > [["Eren", "Jaeger"], ["Mikasa", "Ackerman"]] | |
heroes.flatMap(splitBySpace) // > ["Eren", "Jaeger", "Mikasa", "Ackerman"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment