Skip to content

Instantly share code, notes, and snippets.

@BolajiAyodeji
Last active March 2, 2019 21:53
Show Gist options
  • Save BolajiAyodeji/ce71ff8befb807bf6057c6a01c50fe1c to your computer and use it in GitHub Desktop.
Save BolajiAyodeji/ce71ff8befb807bf6057c6a01c50fe1c to your computer and use it in GitHub Desktop.
Understand filter(), map(), reduce() in 10 seconds
// 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)
@BolajiAyodeji
Copy link
Author

Copy the above code into your console and run to see the results.
If you have further questions, feel free to ask! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment