Skip to content

Instantly share code, notes, and snippets.

@DMcP89
Created May 8, 2024 15:11
Show Gist options
  • Save DMcP89/0e7265e97bc758336eaf0b59ab7b3adc to your computer and use it in GitHub Desktop.
Save DMcP89/0e7265e97bc758336eaf0b59ab7b3adc to your computer and use it in GitHub Desktop.
Dig through object to find target key
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;
};
@DMcP89
Copy link
Author

DMcP89 commented May 8, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment