Skip to content

Instantly share code, notes, and snippets.

@doublejosh
Created May 25, 2016 19:29
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/76f41d9c2889d6e496c56f73c9f0d0af to your computer and use it in GitHub Desktop.
Save doublejosh/76f41d9c2889d6e496c56f73c9f0d0af to your computer and use it in GitHub Desktop.
/**
* Try properties against object and use first available value.
*
* @param {array} properties
* @param {object} obj
*
* @return {string|boolean}
*/
_.firstObj = function (properties, obj) {
var prop = _.first(_.intersection(properties, _.keys(obj)));
return (prop in obj) ? obj[prop] : false;
};
/**
* Example...
*/
console.log(_.firstObj(['name', 'username'], {id: 555, name: 'Bob Smith', username: 'bob123'});
//=> Hello Bob!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment