Skip to content

Instantly share code, notes, and snippets.

@megabayt
Last active December 3, 2019 13:33
Show Gist options
  • Save megabayt/25f7b0adc9293081ab3928f50c23b3c1 to your computer and use it in GitHub Desktop.
Save megabayt/25f7b0adc9293081ab3928f50c23b3c1 to your computer and use it in GitHub Desktop.
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