Skip to content

Instantly share code, notes, and snippets.

@robvolk
Last active November 3, 2016 23:23
Show Gist options
  • Save robvolk/a7e4a0804e7f945d4c7c3a2e8c32ff01 to your computer and use it in GitHub Desktop.
Save robvolk/a7e4a0804e7f945d4c7c3a2e8c32ff01 to your computer and use it in GitHub Desktop.
Determine if a hash has any of a set of values defined using sweet ES6 functional programming methods #map and #reduce
function hasValues(hash, fields) {
return fields.map(field => {
return hash[field] != null;
}).reduce((x, y) => { return x || y });
}
hash = { name: "robert paulson", affiliation: "fightclub" };
hasValues(hash, [ "name", "email" ]); // => true
hasValues(hash, [ "email" ]); // => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment