Created
December 15, 2018 19:37
-
-
Save Vincent-gv/258e41d5bdad994f950afa38ea81bc4f to your computer and use it in GitHub Desktop.
.forEach() .map() .filter() .reduce()
This file contains 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
// .forEach() | |
const artists = ['Picasso', 'Kahlo', 'Matisse', 'Utamaro']; | |
artists.forEach(artist => { | |
console.log(artist + ' is one of my favorite artists.'); | |
}); | |
// expected output: | |
// "Picasso is one of my favorite artists." | |
// "Kahlo is one of my favorite artists." | |
// "Matisse is one of my favorite artists." | |
// "Utamaro is one of my favorite artists." | |
// .map() | |
const numbers = [1, 2, 3, 4, 5]; | |
const squareNumbers = numbers.map(number => { | |
return number * number; | |
}); | |
console.log(squareNumbers); | |
// expected output: [1, 4, 9, 16, 25] | |
// .filter() | |
const things = ['desk', 'chair', 5, 'backpack', 3.14, 100]; | |
const onlyNumbers = things.filter(thing => { | |
return typeof thing === 'number'; | |
}); | |
console.log(onlyNumbers); | |
// expected output: [5, 3.14, 100] | |
// .reduce() | |
const array1 = [1, 2, 3, 4]; | |
const reducer = (accumulator, currentValue) => accumulator + currentValue; | |
// 1 + 2 + 3 + 4 | |
console.log(array1.reduce(reducer)); | |
// expected output: 10 | |
// 5 + 1 + 2 + 3 + 4 | |
console.log(array1.reduce(reducer, 5)); | |
// expected output: 15 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment