Created
April 23, 2018 16:00
-
-
Save keif/296114e786f795a24ad70a3c3c5a3880 to your computer and use it in GitHub Desktop.
Better property detection using underscore
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
// _.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