Skip to content

Instantly share code, notes, and snippets.

@rahulkumar-aws
Created September 21, 2015 09:19
Show Gist options
  • Save rahulkumar-aws/2ac98e913941295f7191 to your computer and use it in GitHub Desktop.
Save rahulkumar-aws/2ac98e913941295f7191 to your computer and use it in GitHub Desktop.
Javascript flatMap implementation
Array.prototype.flatMap = function(lambda) {
return Array.prototype.concat.apply([], this.map(lambda));
};
Example
[0, 1, 2, 3, 4, 5].flatMap(function(x) {
return [x, x + 1];
});
// [0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment