Most of the time this searches entire objects within milliseconds or even seconds. However, sometimes this script can take hours or even days to search in objects.
Here is my code:
const varFind = function (predicate, object) {
const results = [];
const isElement = function (element) {
return element instanceof Element || element instanceof HTMLDocument;
}
const varSearch = function (obj, path, cyclicDetect) {
if(typeof(path) == "undefined") {
path = [
"window"
];
cyclicDetect = [];
}
for (var key of Object.keys(obj)) {
path.push(key);
if(predicate(key, obj, path) === true) {
var editedPath = [...path];
for (var i in path) {
if (i != 0) {
editedPath[i] = "['" + editedPath[i] + "']";
}
}
results.push(editedPath.join(""));
}
var isCyclic = false;
const o = obj[key];
if (o && typeof o === "object" && !isElement(o)) {
cyclicDetect.push(obj);
for(var i in cyclicDetect) {
if(cyclicDetect[i] == o) {
isCyclic = true;
}
}
if (!isCyclic) {
varSearch(o, path, cyclicDetect);
}
cyclicDetect.pop(obj);
}
path.pop();
}
}
varSearch(object || window)
return results;
}
I have tried removing cyclicDetect.pop(obj)
but that did not seem to help, I guess because that variable could get backed up with tons of sub-objects.
I could not think of anything else to try.
Example of use:
var object = {obj: [{x: 27}, {x: 28}]};
var results = varFind((key, obj, path) = > obj[key] == 27);
console.log(results);
//output: ["window['obj']['0']['x']"]
or more commonly
var object = {obj: [{x: 27}, {x: 28}]};
var results = varFind((key, obj, path) = > key == "keyName");
console.log(results);
//output: ["window['obj']['0']['x']", "window['obj']['1']['x']"]
Thanks in advance!