Skip to content

Instantly share code, notes, and snippets.

@umutbasal
Created January 23, 2022 10:54
Show Gist options
  • Save umutbasal/75d0adb77a55471ce0ef9855e212ce72 to your computer and use it in GitHub Desktop.
Save umutbasal/75d0adb77a55471ce0ef9855e212ce72 to your computer and use it in GitHub Desktop.
function logFromTraversal(object, match_string) {
function* traverse(o, path = []) {
for (var i in o) {
const itemPath = path.concat(i);
yield [i, o[i], itemPath, o];
if (o[i] !== null && typeof (o[i]) == "object") {
yield* traverse(o[i], itemPath);
}
}
}
for (var [key, value, path, parent] of traverse(JSON.parse(JSON.stringify(object)))) {
if (typeof value == "string" && value.includes(match_string)) {
console.log({
key,
value,
path: path.join("."),
parent
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment