How to print javascript object properties
Sometime we need to print the structure data that return by a function. I was work on a task for google map and need to print object properties of Placemarks object on javascript. I need to analyze what kind of structure data return by geocoding function. The code below is a short function to print object properties in javascript
function isObject(obj) {
return obj.constructor == Object;
}function print_object(object, level){
var tab = “”;
if(level > 0){
for(i = 0; i < level; i++) tab += “\t”;
}
var str = “”;
for(prop in object) {
if(!isObject(object[prop])) str += tab + prop + ” value :” + object[prop] + “\n”;
else str += tab + prop + “\n” + print_object(object[prop], level + 1);
}
return str;
}
and how to use this function is really simple approach
address = addresses.Placemark[0];
alert(print_object(address, 0));
in this scenario, i would like to print first placemark object return by geocoding function. Parameter level is use to make nice print view using tab character.