简体   繁体   中英

How to search an entire JavaScript object in Firebug?

I am using Firebug in Firefox. I have a giant JavaScript object that holds a lot of other JavaScript objects that in turn also hold objects. I want to find a unique value that could be in any one of these objects. However, Firebug's search field only works on text that's visible inside the DOM panel. When on the list of JavaScript objects, you can click on the plus sign next to it to expand it. However, if an object is not expanded, it seems invisible to the search field.

How do I search for items that are not currently displayed within Firebug's DOM panel?

Firebug's DOM panel currently (Firebug 2.0.4) doesn't support searching within nested objects. This is a long-standing problem and is reported as issue 1545 .

What you can do to circumvent this, is to write your own search function, which returns the result you expect. A simple example for this is the following code:

function searchValue(obj, value) {
  for (var prop in obj) {
    if (typeof obj[prop] === "object") {
      var result = searchValue(obj[prop], value);
      if (result !== '')
        return prop + "." + result;
    }

    if (obj[prop] === value)
      return prop + "." + obj[prop];
  }

  return '';
}

This function does a simple search through the object and returns the path to the property that holds the first occurrence of the value. Eg calling searchValue(test, "bar") on an object defined as test = {a: "foo", b: {c: {d: "bar"}}} returns "bcd" . Note that this function doesn't handle recursions, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM