首页 > 编程知识 正文

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

时间:2023-12-19 12:29:23 阅读:317668 作者:DSGY

本文目录一览:

为什么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);

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

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。