Created
July 9, 2016 22:11
-
-
Save bernbecht/0029aba4dee6a3b76fc554ff3e109492 to your computer and use it in GitHub Desktop.
Map, filter and reduce for functional programming
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
var people = [ | |
{'name': 'Mary', 'height': 160}, | |
{'name': 'Isla', 'height': 80}, | |
{'name': 'Sam'} | |
]; | |
var peopleWithHeight = people.filter(function(person) { | |
return person.hasOwnProperty('height'); | |
}); | |
var heightTotal = peopleWithHeight | |
.map(function(person){ | |
return person.height; | |
}) | |
.reduce(function(accumulator, current){ | |
return accumulator + current; | |
}, 0); | |
console.log(heightTotal/peopleWithHeight.length); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment