`I have written a js function to traverse a javascript object and output it’s contents with console.log(). It recursively calls itself when a property that is an object is encountered. The problem is in the recursive loops, the passed in property doesn’t show any child properties.
Take the sample below (also at JS Fiddle), once the jsObject.payload is passed into the recursive call, ‘payload’ appears to revert to a simple string object.
var EnumerateObject = function(object, path) {
if (!path) path = '';
for (var property in object) {
if (object.hasOwnProperty(property)) {
if (typeof object[property] === "object") EnumerateObject(property, path + '.' + property);
else console.log(path + '.' + property + '=' + typeof property);
}
}
}
var json = '{"success": true, "error": "", "payload": { "fetch": "1", "xml": "<xml />" }}';
var jsObject = $.parseJSON(json);
EnumerateObject(jsObject);
I know I am missing something subtle here, but I am not sure why my passed in ‘property’ to the ‘object’ parameter suddenly becomes a string?
property is a string containing the value “payload”.
object[property]is{'fetch':'1', 'xml':.... }