Skip to content

Instantly share code, notes, and snippets.

@bernbecht
Created July 9, 2016 22:11
Show Gist options
  • Save bernbecht/0029aba4dee6a3b76fc554ff3e109492 to your computer and use it in GitHub Desktop.
Save bernbecht/0029aba4dee6a3b76fc554ff3e109492 to your computer and use it in GitHub Desktop.
Map, filter and reduce for functional programming
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