Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function search(obj, searchedValue, path = '') {
- if (Array.isArray(obj)) {
- obj = Object.assign({}, obj);
- }
- const found = Object.entries(obj).find(([key, value]) => {
- if (value === searchedValue) {
- path += `/${key}`;
- return true;
- }
- if (
- Array.isArray(value)
- || (typeof value === 'object' && value !== null)
- ) {
- const returnedPath = search(value, searchedValue, `${path}/${key}`);
- if (returnedPath !== false) {
- path = returnedPath;
- return true;
- }
- }
- return false;
- });
- return found === undefined ? false : path;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement