Skip to content

Instantly share code, notes, and snippets.

@leandroandrade
Created April 26, 2024 18:04
Show Gist options
  • Save leandroandrade/9728c2e8a59604ccd5e07daf1a9af7f2 to your computer and use it in GitHub Desktop.
Save leandroandrade/9728c2e8a59604ccd5e07daf1a9af7f2 to your computer and use it in GitHub Desktop.
function getValueByKeyJson(obj, key) {
if (obj === null || typeof obj !== 'object') {
return undefined;
}
if (obj.hasOwnProperty(key)) {
return obj[key];
}
for (const property in obj) {
if (obj.hasOwnProperty(property)) {
const found = getValueByKeyJson(obj[property], key);
if (found !== undefined) {
return found;
}
}
}
return undefined;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment