Guest User

Untitled

a guest
Jul 23rd, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. const arr = [1,2,3,4,5];
  2.  
  3. // map applies a function to each element, and returns a new array
  4. const mappedArray = arr.map(element => element + 1); // returns a new array: [2,3,4,5,6]
  5.  
  6. // filter returns a new array consisting of elements which pass the test function
  7. // in this, filter out the odd numbers
  8. const filteredArray = mappedArray.filter(element => element % 2 === 0); // returns a new array: [2,4,6]
  9.  
  10. // go ahead and log them to the screen!
  11. // forEach loops through an array, and calls the function once for every element in the array
  12. filteredArray.forEach(element => console.log(element));
  13. // prints: 2, 4, 6
  14.  
  15. // but arrays are composable, so you can also do:
  16. arr.map(element => element + 1)
  17. .filter(element => element % 2 === 0)
  18. .forEach(element => console.log(element))
Add Comment
Please, Sign In to add comment