Last active
November 19, 2017 18:18
-
-
Save cppio/132579c07c3b3c863e2047a6aea23504 to your computer and use it in GitHub Desktop.
A function to find a string in an object.
This file contains hidden or 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 findString(object, string, seen, path) { | |
seen = seen || [object]; | |
path = path || ""; | |
for (var i in object) { | |
try { object[i]; } catch (e) { continue; } | |
if (!seen.includes(object[i])) { | |
seen.push(object[i]); | |
if (object[i] instanceof Object && !(object[i] instanceof HTMLElement)) | |
findString(object[i], string, seen, path + '["' + i + '"]'); | |
else if (typeof object[i] === "string" && object[i].includes(string)) | |
console.log(path + '["' + i + '"]', object[i]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment