Created
August 31, 2012 23:41
-
-
Save lstebner/3561193 to your computer and use it in GitHub Desktop.
Underscore mixin get object property with default value
This file contains hidden or 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
| //get a key from an object if it exists, otherwise use default | |
| _.mixin({ | |
| get: function(obj, key, default_val){ | |
| if (obj && _.has(obj, key)){ | |
| return obj[key]; | |
| } | |
| else{ | |
| return default_val != undefined ? default_val : ''; | |
| } | |
| } | |
| }); |
Author
Author
updated to check for default_val to not be undefined because it was returning an empty string when default_val was set to false
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
updated to make sure
objexists before calling_.hasto preview an error whenobjis null