Skip to content

Instantly share code, notes, and snippets.

@doublejosh
Last active May 16, 2016 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doublejosh/00c6785e7851451816625bc28345f741 to your computer and use it in GitHub Desktop.
Save doublejosh/00c6785e7851451816625bc28345f741 to your computer and use it in GitHub Desktop.
/**
* Try properties against object and map first value with callback.
*
* @param {array} properties
* @param {object} obj
* @param {function} callback
*
* @return {string|boolean}
*/
_.mapFirst = function (properties, obj, callback) {
var prop = _.first(_.intersection(properties, _.keys(obj)));
return (prop in obj) ? callback(obj[prop]) : false;
};
/**
* Example...
*/
console.log(_.mapFirst(['first', 'username'], {id: 555, first: 'Bob', username: 'bob123'}, function (value) {
return 'Hello ' + value + '!';
});
//=> Hello Bob!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment