Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to print object objects in JavaScript

Shulou Source: shulou.com Published: 2022-06-01 16:42:19 10月19日 Update

JavaScript中怎么打印object对象,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

具体代码如下:

function writeObj(obj){ var description = ""; for(var i in obj){ var property=obj[i]; description+=i+" = "+property+"\n"; } alert(description); }

另外当你需要将object对象转换为string字符串。

有下面这个函数就好了,可以将其转化为字符串类型,然后就可以打印出来了,具体代码如下:

functionobj2string(o){ varr=[]; if(typeofo=="string"){ return"\""+o.replace(/([\'\"\\])/g,"\\$1").replace(/(\n)/g,"\\n").replace(/(\r)/g,"\\r").replace(/(\t)/g,"\\t")+"\""; } if(typeofo=="object"){ if(!o.sort){ for(variino){ r.push(i+":"+obj2string(o[i])); } if(!!document.all&&!/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)){ r.push("toString:"+o.toString.toString()); } r="{"+r.join()+"}"; }else{ for(vari=0;i

Tags: Object code content character string method more problem helpless for this function reason skill article type experience industry information information channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft OPPO Reno Shulou Tech Info Xiaomi Huawei