Created
February 26, 2018 20:30
-
-
Save geekdenz/4579af3dc399892680a0e53cdf87c71a to your computer and use it in GitHub Desktop.
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
interface Array<T> { | |
flatMap: () => T[] | |
} | |
Array.prototype.flatMap = function() { | |
function innerFlatMap(arr) { | |
if (arr instanceof Array) { | |
return arr.reduce((r, e) => r.concat(innerFlatMap(e)), []); | |
} | |
return arr; | |
} | |
return innerFlatMap(this); | |
}; | |
// console.log([1,2,4,[5,7,[[1,2],3]]].flatMap()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment