js实例对象打印(js 打印对象)

本文目录一览:

为什么node.js中有的对象不能够打印出来

对象只能打印内存地址,具体属性指定才可以打印具体的对象信息

求一个js方法,能够打印任何对象所有属性值。最好有参数可指定子对象的层次。 好用有现金红包报酬,多

代码贴上

script

/**

需求1:打印对象里面所有属性

需求2:可以指定该对象里面子对象的打印层次

*/

function isObject(item) {

return Object.prototype.toString.call(item) == ‘[object Object]’;

}

var index = 0;

function printItems(items, n) {

var printStr = ‘{\n’;

for(var item in items) {

if(isObject(items[item]) index n) {

index++;

printStr += item + “:” ;

printStr += printItems(items[item],n);

index = 0;

}else {

printStr += item + ‘:’ + items[item] + ”;

}

printStr += ‘,\n’

}

printStr += ‘\n}’;

return printStr;

}

var n = 3;

var item = {

name: ‘第一层名称’,

onechild: {name: ‘onchild名称’,twochild: {name:’twochild名称’, threechild:{name: ‘threechild名称’,age:’108′}}},

tchild: {name: ‘onchild名称’,twochild: {name:’twochild名称’, threechild:{name: ‘threechild名称’,age: ‘108’}}},

rnechild: {name: ‘onchild名称’,twochild: {name:’twochild名称’, threechild:{name: ‘threechild名称’,age: ‘108’}}}

}

var print = printItems(item,n);

alert(print);

/script

js怎么打印object object

json格式有数组和对象2种结构 数组用下标就可以得到,2个中括号括起来的是数组 对象就是用大括号括起来的

js中怎样打印结果?

能打印,首先你要有一个带控制台的浏览器,比如说chrome、firefox、或者基于chrome内核的浏览器,ie9+也可以。

f12打开控制台,切换到“console”选项卡或者叫“控制台”

js中这么写

var a = 1;

console.log(a);

这样程序运行的时候,就会打印到控制台了。不管是数组对象 都能有一个良好的体现

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/249412.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝的头像小蓝
上一篇 2024-12-12 17:12
下一篇 2024-12-12 17:12

相关推荐

发表回复

登录后才能评论