Created
April 26, 2024 18:04
-
-
Save leandroandrade/9728c2e8a59604ccd5e07daf1a9af7f2 to your computer and use it in GitHub Desktop.
This file contains 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 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