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?
We can check if a property exists in the object by checking if property !== undefined . In this example, it would return true because the name property does exist in the developer object.
There are mainly two methods to check the existence of a key in JavaScript Object. The first one is using “in operator” and the second one is using “hasOwnProperty() method”. Method 1: Using 'in' operator: The in operator returns a boolean value if the specified property is in the object.
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.
This function will Search in Object. It’ll match Search Query with Object’s every property.This is useful when you need to search in multidimensional object After spending hours I got this code from Google’s AngularJS Project.
/* Seach in Object */ var comparator = function(obj, text) { if (obj && text && typeof obj === 'object' && typeof text === 'object') { for (var objKey in obj) { if (objKey.charAt(0) !== '$' && hasOwnProperty.call(obj, objKey) && comparator(obj[objKey], text[objKey])) { return true; } } return false; } text = ('' + text).toLowerCase(); return ('' + obj).toLowerCase().indexOf(text) > -1; }; var search = function(obj, text) { if (typeof text == 'string' && text.charAt(0) === '!') { return !search(obj, text.substr(1)); } switch (typeof obj) { case "boolean": case "number": case "string": return comparator(obj, text); case "object": switch (typeof text) { case "object": return comparator(obj, text); default: for (var objKey in obj) { if (objKey.charAt(0) !== '$' && search(obj[objKey], text)) { return true; } } break; } return false; case "array": for (var i = 0; i < obj.length; i++) { if (search(obj[i], text)) { return true; } } return false; default: return false; } };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With