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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
updated to check for
default_valto not beundefinedbecause it was returning an empty string whendefault_valwas set tofalse