Skip to content

Instantly share code, notes, and snippets.

@osiyuk
Created November 13, 2016 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osiyuk/361f5fe808073f6ce8aec4d94c446863 to your computer and use it in GitHub Desktop.
Save osiyuk/361f5fe808073f6ce8aec4d94c446863 to your computer and use it in GitHub Desktop.
something, that lodash already does
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