Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
117
rated 0 times [  123] [ 6]  / answers: 1 / hits: 66078  / 13 Years ago, thu, february 23, 2012, 12:00:00

I have a javascript object, and I want to recursively search it to find any properties that contain a specific value.



The javascript I'm working with has been minified, and is not so easy to trace through.



Background



I'm using the Bing Maps AJAX SDK. It has the ability to add additional tile layers. Each tilelayer has a tilesource object, which specifies the URI format for the tile URL.



I've ran into a problem where the tilesource URI is created once, and cached. Thus I can't dynamically change the parameters of the URL (for example, to change the colors of the tile overlay based on the time of day) for each request.



Note that this behavior is different that Google's Map API, and the Bing Maps api for WP7, which both allow you to dynamically create the URL for each tile request.



The cached URI is looked up, and two specific parameters are replaced, then the URI is used to fetch the tile.



Since this is javascript, I'd like to find the cached URI, and replace it with a function, that instead dynamically builds the URI, and returns it.



I don't need to do this each runtime, just want and idea of where the property is being cached, so I can write code to hax0r it.



Original Question



If I set the URI to some value like floobieblaster, when I set a breakpoint, can I search the javascript object recursively for floobieblaster and get the property that is storing that value?



Edit to add



The object I'm searching appears to have a circular reference, thus any recursive code will likely cause a stackoverflow.



Are there any editor/debugger tricks I could make use of?


More From » javascript

 Answers
68

Something simple like this should work:



var testObj = {
test: 'testValue',
test1: 'testValue1',
test2: {
test2a: 'testValue',
test2b: 'testValue1'
}
}

function searchObj (obj, query) {

for (var key in obj) {
var value = obj[key];

if (typeof value === 'object') {
searchObj(value, query);
}

if (value === query) {
console.log('property=' + key + ' value=' + value);
}

}

}


If you execute searchObj(testObj, 'testValue'); it will log the following to the console:



property=test value=testValue
property=test2a value=testValue


Obviously, you can replace the console.log with whatever you want, or add a callback parameter to the searchObj function to make it more reusable.



EDIT: Added the query parameter which allows you to specify the value you want to search for when you call the function.


[#87248] Thursday, February 23, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jenamackennac

Total Points: 304
Total Questions: 110
Total Answers: 107

Location: Ecuador
Member since Thu, Jun 4, 2020
4 Years ago
jenamackennac questions
Fri, Feb 18, 22, 00:00, 2 Years ago
Wed, Apr 21, 21, 00:00, 3 Years ago
Thu, Apr 1, 21, 00:00, 3 Years ago
Tue, Feb 2, 21, 00:00, 3 Years ago
;