Skip to content

Instantly share code, notes, and snippets.

@keif
Created April 23, 2018 16:00
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 keif/296114e786f795a24ad70a3c3c5a3880 to your computer and use it in GitHub Desktop.
Save keif/296114e786f795a24ad70a3c3c5a3880 to your computer and use it in GitHub Desktop.
Better property detection using underscore
// _.propertyOf along with any other underscore (i.e. _.isObject) to check if something was available instead of
// doing an undefined and null check.
_.isObject(_.propertyOf(data.prop)("prop"));
// _.propertyOf returns a function so you can also assign that to a variable and then use it later as well
var dataPropOf = _.propertyOf(data.prop);
_.isObject(dataPropOf("prop"));
// The exact same technique can be used if the property was an array:
_.isArray(_.propertyOf(data.prop)("prop"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment