o2s(myvar);
multiple variables:
o2s([myvar1,myvar2]);
o2s([myobj,myarr]);
It's small enough that I can just post it here
o2s=function(o){//Simple object parser
ostr="";
function getO(o){
switch(typeof o){
case "undefined": case "function":
ostr += "'"+typeof o+"'";
break;case "boolean":
ostr += (o)?"true":"false";
break;case "number":
ostr += o;
break;case "string":
ostr += "'"+o+"'";
break;case "object":
if(o == null){
ostr += "null";
}else if(o instanceof Array){
ostr += "[";
for(var i=0,il=o.length;i<il;i++){
getO(o[i]);
ostr += ", ";
}
ostr=ostr.slice(0,-2)+"]";
}else{
ostr += "{";
for (var key in o){
ostr += "'"+key+"': ";
getO(o[key]);
ostr += ", ";
}
ostr=ostr.slice(0,-2)+"}";
}
break;
}
}
getO(o);
console.log(ostr);
}
If you are using another browser, you can just set it to alert, rather than console.log on the last line
0 Comments
Post New Comment