简体   繁体   English

JavaScript 循环遍历所有现有对象以找到 object 密钥

[英]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.

相关问题 JavaScript-遍历对象以查找特定的键值 - JavaScript - Loop through Object to find Specific Key Value Javascript:使用不规则的键遍历对象内的所有记录 - Javascript: loop through all records within an object with an irregular key 使用数组作为键来遍历JavaScript中的对象 - using array as key to loop through objects in JavaScript 使用 switch case 和过滤/查找/循环对象数组来过滤并找到正确的 object 以更新 JavaScript - Use switch case and filter/find/loop through array of objects to filter and find the correct object to update JavaScript 我在Javascript中有一个对象数组。如何遍历它以查找&#39;fname&#39;键的出现次数 - I have an array of objects in Javascript.How to loop through it to find the number of occurances of 'fname' key JavaScript:在对象数组中循环并使用reduce函数查找特定键的总和 - JavaScript: Loop through Array of Objects and Find Sum of Specific Key Using Reduce Function 遍历对象并获取其他对象(javascript) - Loop through object and get other objects (javascript) Javascript 对象 - 检查并循环遍历对象中的数组 - Javascript Objects - Check for and loop through arrays in object Javascript遍历许多对象的对象 - Javascript loop through object of many objects 循环通过 JavaScript Object 和子对象 - Loop through JavaScript Object and sub Objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM