Last active
December 3, 2019 13:33
-
-
Save megabayt/25f7b0adc9293081ab3928f50c23b3c1 to your computer and use it in GitHub Desktop.
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 search(obj, value, path = '') { | |
if (!obj) { | |
return undefined; | |
} | |
if (Array.isArray(obj)) { | |
let found; | |
obj.some((item, index) => { | |
found = search(item, value, `${path}[${index}]`); | |
return found; | |
}); | |
if (found) { | |
return found; | |
} | |
} | |
if (typeof obj === 'object') { | |
const keys = Object.keys(obj); | |
let found; | |
keys.some(key => { | |
found = search(obj[key], value, `${path}.${key}`); | |
return found; | |
}); | |
if (found) { | |
return found; | |
} | |
} | |
if (value === obj) { | |
return path; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment