Last active
December 15, 2017 13:11
-
-
Save davidhellsing/01ecaecd4245441a81280310f7e847bc to your computer and use it in GitHub Desktop.
Haystack needle recursive
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
findKey = (haystack, needle) => { | |
let found = false | |
const iterate = (obj) => ( | |
Object.keys(obj).forEach((key) => { | |
if (found === false) { | |
if (key === needle) { | |
found = key | |
} else if (obj[key] && typeof obj[key] === 'object') { | |
iterate(obj[key]) | |
} | |
} | |
}) | |
) | |
if (haystack && typeof haystack === 'object') { | |
iterate(haystack) | |
} | |
return found | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment