Created
May 8, 2024 15:11
-
-
Save DMcP89/0e7265e97bc758336eaf0b59ab7b3adc to your computer and use it in GitHub Desktop.
Dig through object to find target key
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
const dig = (obj, target) => { | |
if (target in obj) { | |
return obj[target]; | |
} | |
for (const key in obj) { | |
if (typeof obj[key] === 'object') { | |
const result = dig(obj[key], target); | |
if (result !== undefined) { | |
return result; | |
} | |
} | |
} | |
return undefined; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.30secondsofcode.org/js/s/get-nested-object-value/#search-for-a-deeply-nested-property-in-an-object