Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- arr = [1,2,3,4,5]
- function map(arr,fn) {
- res = [];
- for(i in arr) {
- res.push(fn(arr[i]));
- }
- return res;
- }
- // maps the function and the flattens it to a depth of one.
- function flatMap(arr,fn) {
- return Array.prototype.concat.apply([],map(arr,fn));
- }
- console.log(map(arr,x=>[2*x])); // [ [ 2 ], [ 4 ], [ 6 ], [ 8 ], [ 10 ] ]
- console.log(flatMap(arr,x=>[2*x])); // [ 2, 4, 6, 8, 10 ]
Add Comment
Please, Sign In to add comment