Skip to content

Instantly share code, notes, and snippets.

@wmaurer
Created December 12, 2015 18:04
Show Gist options
  • Save wmaurer/9a13e01299b145e22929 to your computer and use it in GitHub Desktop.
Save wmaurer/9a13e01299b145e22929 to your computer and use it in GitHub Desktop.
flatMap function on Array.prototype
interface Array<T> {
flatMap<TResult>(selector: (source: T) => Array<TResult>): Array<TResult>;
}
Array.prototype.flatMap = function<T, TResult>(selector: (source: T) => Array<TResult>) {
return (this as Array<T>).map(value => selector(value)).reduce((acc, value) => {
return acc.concat(value);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment