Skip to content

Instantly share code, notes, and snippets.

@DominicTremblay
Created January 29, 2018 19:16
Show Gist options
  • Save DominicTremblay/fa4af7125bf99fa11b2352c05fd0eddc to your computer and use it in GitHub Desktop.
Save DominicTremblay/fa4af7125bf99fa11b2352c05fd0eddc to your computer and use it in GitHub Desktop.
Breakout about higher order functions
const ancestry = [
{"name": "Emma de Milliano", "sex": "f",
"born": 1876, "died": 1956,
"father": "Petrus de Milliano",
"mother": "Sophia van Damme"},
{"name": "Carolus Haverbeke", "sex": "m",
"born": 1832, "died": 1905,
"father": "Carel Haverbeke",
"mother": "Maria van Brussel"},
{"name": "Marcus Pillus", "sex": "m",
"born": 1901, "died": 1905,
"father": "Carel Haverbeke",
"mother": "Maria van Brussel"}
]
function filter(array, test ) {
const filteredArray = []
for (let element of array) {
// element contains each object one at a time
if (test(element)) {
// if (element.born > 1900 && element.born < 1925) {
filteredArray.push(element);
}
}
return filteredArray;
}
function map(array, transform) {
// const transform = function(inputObj) { return inputObj.name};
const mappedArray = [];
for (let element of array) {
mappedArray.push( transform(element) );
}
return mappedArray;
}
const over50 = filter(ancestry, function(inputObj){
return inputObj.died - inputObj.born > 50;
})
console.log(map(over50, function(inputObj) { return inputObj.name} ));
// ouput the names of the persons over 90
// ["Clara Aernoudts", "Emile Haverbeke",
// "Maria Haverbeke"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment