Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const arr = [1,2,3,4,5];
- // map applies a function to each element, and returns a new array
- const mappedArray = arr.map(element => element + 1); // returns a new array: [2,3,4,5,6]
- // filter returns a new array consisting of elements which pass the test function
- // in this, filter out the odd numbers
- const filteredArray = mappedArray.filter(element => element % 2 === 0); // returns a new array: [2,4,6]
- // go ahead and log them to the screen!
- // forEach loops through an array, and calls the function once for every element in the array
- filteredArray.forEach(element => console.log(element));
- // prints: 2, 4, 6
- // but arrays are composable, so you can also do:
- arr.map(element => element + 1)
- .filter(element => element % 2 === 0)
- .forEach(element => console.log(element))
Add Comment
Please, Sign In to add comment