0

I want to traverse the object graph starting at a specific root object and find which path, starting from it, leads to an object with a property that has a given value. This is my code so far:

function findPropertyValue(obj, value) { 
  if (typeof obj.seenBefore === "undefined") {
    // treat for object graph circularity
    obj.seenBefore = true; 

    for (var key in obj) {
      if (obj[key] == value) {
        return key;
      } else {
        if (obj[key]) {
          var foundIt = findInput(obj[key], value);
          if (foundIt) {
            return key + '.' + foundIt;
          }
        }
      }
    }
  }
  return false;
};

The problem with it is that it very quickly hits the call stack size limit on Chrome and is unable to continue with the search. Is there another way to do this OR to increase the stack size limit only for debugging purposes?

agentofuser
  • 8,987
  • 11
  • 54
  • 85
  • Why do you need such a thing? What is it you are trying to do? – phant0m Nov 25 '12 at 13:10
  • The type of search you describe can rapidly explode into a huge memory and time consumer, as it is essentially an unbounded search--especially if you don't have protections against cycles in the graph (you set `seenBefore` but never check it, so it is useless). I echo the previous comment--what are you actually trying to achieve? – SAJ14SAJ Nov 25 '12 at 13:12
  • 1
    Related: http://stackoverflow.com/q/12438175/1615483 – Paul S. Nov 25 '12 at 13:19
  • @SAJ14SAJ "never using" `seenBefore` is the whole point. The function does nothing if the object has been seen before, including traverse it, so it doesn't go into cycles. The first `if` is where it's used. – agentofuser Nov 25 '12 at 13:22
  • @phant0m I'm trying to get the current input field value from the search box in a Google Images search results page from inside a Content Script on Chrome. For some weird reason, the `value` attribute of the input DOM object remains unchanged if you do subsequent searches on the same results page, so I have to find where the current one is being kept. – agentofuser Nov 25 '12 at 13:24
  • 1
    Sorry, I missed the check before the set. – SAJ14SAJ Nov 25 '12 at 13:26
  • @m02ph3u5 sorry, I changed the original function name and forgot to do it at that spot. Fixed, thanks! – agentofuser Nov 25 '12 at 13:29
  • @phant0m ok, in the special case of the google SERP I found I was being very stupid and the current search is repeated in the title tag and many other links around the page (just not in the input!), so I found a work around for the specific problem. The general one still interests me though and it would be nice to find a solution. – agentofuser Nov 25 '12 at 13:30
  • 1
    In that case, I suggest you ask your question about that exact problem. This seems to be a common problem on SO with questions. People have a problem, they think up some possibly flawed solution and get stuck, then they try to fix their solution no matter what instead of looking at the real problem they have. – phant0m Nov 25 '12 at 13:32
  • 1
    Your code should use `var key`, as well as pass `value` through to the recursive function. – pimvdb Nov 25 '12 at 13:35
  • @pimvdb thanks, I had the value hardcoded in the function and generalized it to post here. thanks for finding that error. – agentofuser Nov 25 '12 at 13:37
  • @phant0m You're right about that problem and I'm aware of it. See my comment above yours. – agentofuser Nov 25 '12 at 13:38

0 Answers0