Guest User

Untitled

a guest
Jul 15th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.40 KB | None | 0 0
  1. arr = [1,2,3,4,5]
  2.  
  3. function map(arr,fn) {
  4. res = [];
  5. for(i in arr) {
  6. res.push(fn(arr[i]));
  7. }
  8. return res;
  9. }
  10.  
  11. // maps the function and the flattens it to a depth of one.
  12. function flatMap(arr,fn) {
  13. return Array.prototype.concat.apply([],map(arr,fn));
  14. }
  15.  
  16. console.log(map(arr,x=>[2*x])); // [ [ 2 ], [ 4 ], [ 6 ], [ 8 ], [ 10 ] ]
  17. console.log(flatMap(arr,x=>[2*x])); // [ 2, 4, 6, 8, 10 ]
Add Comment
Please, Sign In to add comment