Created
February 15, 2013 00:44
-
-
Save liammclennan/4957758 to your computer and use it in GitHub Desktop.
mapMany (SelectMany) in JavaScript
This file contains hidden or 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
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