Skip to content

Instantly share code, notes, and snippets.

@liammclennan
Created February 15, 2013 00:44
Show Gist options
  • Save liammclennan/4957758 to your computer and use it in GitHub Desktop.
Save liammclennan/4957758 to your computer and use it in GitHub Desktop.
mapMany (SelectMany) in JavaScript
function mapMany (arr, mapper) {
return arr.reduce(function (prev, curr, i) {
return prev.concat(mapper(curr));
},[]);
}
// usage
var res1 = mapMany(['abcd', '1234'], function (item) {
return item.split('');
});
console.log(res1);
// --> ["a", "b", "c", "d", "1", "2", "3", "4"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment