Skip to content

Instantly share code, notes, and snippets.

@cobbweb
Forked from kakajika/flatMap.ts
Created November 23, 2018 04:15
Show Gist options
  • Save cobbweb/e939210541b587374fc0acb6df508950 to your computer and use it in GitHub Desktop.
Save cobbweb/e939210541b587374fc0acb6df508950 to your computer and use it in GitHub Desktop.
Array.prototype.flatMap method in TypeScript.
interface Array<T> {
flatMap<E>(callback: (t: T) => Array<E>): Array<E>
}
Object.defineProperty(Array.prototype, 'flatMap', {
value: function(f: Function) {
return this.reduce((ys: any, x: any) => {
return ys.concat(f.call(this, x))
}, [])
},
enumerable: false,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment