Skip to content

Instantly share code, notes, and snippets.

@nashvail
Last active January 2, 2018 05:52
Show Gist options
  • Save nashvail/dec37b7339fe2efd117caef38d95640a to your computer and use it in GitHub Desktop.
Save nashvail/dec37b7339fe2efd117caef38d95640a to your computer and use it in GitHub Desktop.
Recursively find prop in nested objects.
function findPropIn(propName, obj) {
if (obj == undefined || typeof obj != 'object') return;
if (propName in obj) { // 🗒️ If propName exists in the prototype chain
return obj[propName];
} else {
for (let prop of Object.keys(obj)) { // 👈🏽 looping through the keys
let ret = findPropIn(propName, obj[prop]);
if (ret !== undefined) {
return ret;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment