Last active
May 15, 2019 22:14
-
-
Save ratiw/55edd04b9cacbef6ff84 to your computer and use it in GitHub Desktop.
Retrieve object property using dot notation. Equivalent to Laravel's object_get()
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
function object_get(obj, key) { | |
if (!key || $.trim(key) == '') return obj; | |
$.each(key.split('.'), function(idx, seg) { | |
if (typeof obj !== 'object' || obj[seg] === undefined) { | |
obj = undefined; | |
return obj; | |
} | |
obj = obj[seg]; | |
}); | |
return obj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment