Last active
March 2, 2019 21:53
-
-
Save BolajiAyodeji/ce71ff8befb807bf6057c6a01c50fe1c to your computer and use it in GitHub Desktop.
Understand filter(), map(), reduce() in 10 seconds
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
// FILTER(), MAP(), REDUCE() | |
const boxs = [1, 3, 5, 8, 6, 7, 9]; | |
// FITER() | |
// This method creates a new array if the items of an array pass a certain condition. | |
const boxFilter = boxs.filter(box => { | |
if (box > 3) { | |
return box; | |
} | |
}) | |
console.log(boxFilter) | |
// MAP() | |
// This method creates a new array by manipulating the values in an array. | |
const boxMap = boxs.map(box => { | |
return '$' + box; | |
}) | |
console.log(boxMap) | |
// REDUCE() | |
// This method is good for calculating totals, it converts an array into a single value. | |
const boxReduce = boxs.reduce((accumulator, value) => { | |
return accumulator + value; | |
}, 0) | |
console.log(boxReduce) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Copy the above code into your console and run to see the results.
If you have further questions, feel free to ask! 👍