Last active
January 2, 2018 05:52
-
-
Save nashvail/dec37b7339fe2efd117caef38d95640a to your computer and use it in GitHub Desktop.
Recursively find prop in nested objects.
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 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