Skip to content

Instantly share code, notes, and snippets.

@davidhellsing
Last active December 15, 2017 13:11
Show Gist options
  • Save davidhellsing/01ecaecd4245441a81280310f7e847bc to your computer and use it in GitHub Desktop.
Save davidhellsing/01ecaecd4245441a81280310f7e847bc to your computer and use it in GitHub Desktop.
Haystack needle recursive
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