Created
June 8, 2012 19:08
-
-
Save daniyalzade/2897639 to your computer and use it in GitHub Desktop.
getDotted
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
function getDotted(data, field, def, delimeter) { | |
delimeter = delimeter || '.'; | |
if (field.indexOf(delimeter) < 0) { | |
return data[field] || def; | |
} | |
var components = field.split(delimeter); | |
components = components.reverse(); | |
while (!!components.length && (typeof(data) == 'object')) { | |
data = data[components.pop()] | |
} | |
// If there are components left, the final dest wasn't reached. | |
if ((!!components.length) || (!data)) { | |
return def | |
} | |
return data | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment