Created
November 13, 2016 17:36
-
-
Save osiyuk/361f5fe808073f6ce8aec4d94c446863 to your computer and use it in GitHub Desktop.
something, that lodash already does
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
var pc = '', | |
ERROR = 'Not found'; | |
// something doing with pc | |
function getKeyInJson(path, json) { | |
var key; | |
if (path === '') { | |
return JSON.stringify(pc); | |
} | |
if (typeof path === 'string') { | |
path = path.split('/'); | |
} | |
key = path.shift(); | |
if (typeof json[key] === 'undefined') { | |
return ERROR; | |
} | |
return path.length ? | |
getKeyInJson(path, json[key]) : | |
JSON.stringify(json[key]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment