我正在使用
console.log(object)
使用 VS 代码终端在 JavaScript (Nodejs) 中打印一个对象。
object = { data: 1, next: { data: 2, next: { data: 3, next: { data: 4, next: null }}}};
console.log(object)
终端显示:
>>> { data: 1, next: { data: 2, next: { data: 3, next: [Object] } } }
如何显示所有对象?我想要这个输出:
>>> { data: 1, next: { data: 2, next: { data: 3, next: { data: 4, next: null }}}}
最简单的方法是首先将对象字符串化。你可以通过调用
console.log(JSON.stringify(object));
来做到这一点。也可以在使用 console.log(JSON.stringify(object, null, 2));
对其进行字符串化的同时漂亮地打印对象(2
意味着它将使用两个空格进行缩进)。
尝试使用 console.dir()。
这是它的文档:https://nodejs.org/api/console.html#consoledirobj-options
你需要传递一个深度值:
> object = { data: 1, next: { data: 2, next: { data: 3, next: { data: 4, next: null }}}};
> console.dir(object,{depth:5});
{
data: 1,
next: { data: 2, next: { data: 3, next: { data: 4, next: null } }
}
}