Created
March 13, 2017 10:44
-
-
Save tjelen/6db300d4ec1afd1f9a8328b2ab920197 to your computer and use it in GitHub Desktop.
Lookup by path (IE9+)
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
'use strict' | |
function lookup(value, path, defaultVal) { | |
defaultVal == null && (defaultVal = null) | |
if (value == null) { | |
return defaultVal | |
} | |
if (path === '.' || path === '') { | |
path = [] | |
} | |
var keys = Array.isArray(path) ? path : path.split('.') | |
for (var i = 0, n = keys.length; i < n; i++) { | |
value = value[keys[i]] | |
if (value == null) { | |
return defaultVal | |
} | |
} | |
return value | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment