Created
October 12, 2018 08:29
-
-
Save LeighCiechanowski/dc49a00582c5226d77ece4b261bcca2e to your computer and use it in GitHub Desktop.
Filter Map Reduce
This file contains hidden or 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
const data = [ | |
{ | |
name: 'Butters', | |
age: 3, | |
type: 'dog' | |
}, | |
{ | |
name: 'Lizzy', | |
age: 6, | |
type: 'dog' | |
}, | |
{ | |
name: 'Red', | |
age: 1, | |
type: 'cat' | |
}, | |
{ | |
name: 'Joey', | |
age: 3, | |
type: 'dog' | |
}, | |
]; | |
const dog = (animal) => animal.type === 'dog'; | |
const dogYears = (animal) => animal.age * 7; | |
const sum = (acc, age) => acc + age; | |
const sumInDogYears = data.filter(dog) | |
.map(dogYears).reduce(sum); | |
console.log(sumInDogYears); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment