Last active
May 16, 2016 00:20
-
-
Save doublejosh/00c6785e7851451816625bc28345f741 to your computer and use it in GitHub Desktop.
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
/** | |
* 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