[英]JavaScript loop through all existing objects to find an object key
I am trying to debug a system that was built by someone else.我正在尝试调试由其他人构建的系统。 I have a minified library that makes an ajax (xhr) call to an external data source and writes the received data to an object or a variable.
我有一个缩小的库,它对外部数据源进行 ajax (xhr) 调用,并将接收到的数据写入 object 或变量。 The call is made immediately after page load so I can't catch it by appending a function to register all XHR call requests.
该调用是在页面加载后立即进行的,因此我无法通过附加 function 来注册所有 XHR 调用请求来捕获它。 So I am trying to run a loop through all variables and objects that the browser window has.
所以我试图循环遍历浏览器 window 拥有的所有变量和对象。 I am trying this:
我正在尝试这个:
var foundVar = false;
loopThroughObject(this);
function loopThroughObject(obj) {
if (!foundVar) {
try {
for (var name in obj) {
if (obj[name] && {}.toString.call(obj[name]) === '[object Function]') { //making sure that the object is not a function
} else {
if (name == 'searchedKey') { //found the object with key = searchedKey
console.log(obj[name]);
foundVar = true;
} else {
setTimeout(loopThroughObject.bind(null, obj[name]), 10); //do more recursion of inner objects
}
}
}
} catch (error) {}
}
}
The problem is that the setTimeout(loopThroughObject.bind(null, obj[name]), 0);
问题是
setTimeout(loopThroughObject.bind(null, obj[name]), 0);
part stacks up and a memory issue occurs.零件堆积起来,并出现 memory 问题。 I tried the simple
loopThroughObject(obj[name])
, but I am facing the "Too much recursion" error after 25000 loops.我尝试了简单的
loopThroughObject(obj[name])
,但在 25000 次循环后我面临“递归过多”错误。 It also seems that I am missing something and it causes the loop go through some object types that I don't need.我似乎也遗漏了一些东西,它通过一些我不需要的 object 类型导致循环 go 。 Does anyone know a more efficient way to do that loop?
有谁知道更有效的方法来做那个循环?
PS I tried a very simple HTML page and the code works fine: PS我尝试了一个非常简单的 HTML 页面,代码工作正常:
<html>
<head>
<script>
var test = JSON.parse("{\"searchedKey\":\"12\"}");
</script>
</head>
<body>
</body>
</html>
The problem is almost certainly the fact that window
has several properties that point back to itself, so your code is going into an infinite loop.几乎可以肯定,问题在于
window
有几个指向自身的属性,因此您的代码将进入无限循环。 There may well be other circular references.很可能还有其他循环引用。
You can remember what objects you've already looked in using a Set
, like this (I've also added a few other tweaks; for instance, you can check for a function with typeof x === "function"
):您可以记住使用
Set
已经查看过的对象,如下所示(我还添加了一些其他调整;例如,您可以使用typeof x === "function"
检查 function ):
let foundVar = false;
let seen = new Set();
loopThroughObject(this);
function loopThroughObject(obj) {
if (!foundVar) {
try {
for (const name in obj) {
const value = obj[name];
if (typeof value !== "function") {
if (name == "searchedKey") { // found the object with key = searchedKey
console.log(value);
foundVar = true;
break;
} else if (value && typeof value === "object" && !seen.has(value)) {
seen.add(value);
loopThroughObject(value); // No need for setTimeout
}
}
}
} catch (error) {}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.